Methods for Adding Columns in pandas
We will use two methods for adding the column names in this guide. These two methods are:
- DataFrame.columns
- DataFrame.set_axis() method
Example 1: Using DataFrame.columns Method
We perform all the given examples on “Spyder” software. We must install the “Spyder” software first, and after installation, we must add some code lines to this software tool. We must import the “pandas as pd”, which we utilize so we will quickly get the methods of pandas by just typing “pd”. After importing this, we are creating the lists below. The first list is named “la”, and we add “Apple”, “Banana”, and “Orange” to the first list. The “lb” is the second list here, and it contains “Onion”, “Bringle”, and “Chilli”. Then comes the third list with the name “lc”, and we put “Pear”, “Mango”, and “StrawBerry” to this third list. Now, we have created the final list “ld”, and here, we add “Tomato”, “Potato”, and “Carrot”.
After creating these lists, we have “pd.DataFrame” for converting them into a DataFrame and put the name of all lists here. Also, we named this DataFrame “listName”. Now, we print this “listName”, and you will see that there is no column name or heading for these columns.
We press “Shift+ Enter”, and this outcome is rendered on the screen. Here, you can observe that no column heading or column name is mentioned because we didn’t add the column name yet. Now, we are moving to add a few new lines in the previous code in which we will add the name of columns on these given columns. Let’s see how we add column names.
Here, we add the DataFrame.column method for adding the column’s name. The name of the DataFrame we have used is “listName”, so we write it as “listName.columns” for adding the column’s name we have created. We put four names in this “listName.columns” for four columns. These names will appear on the top of each column. We print this by utilizing the “print” method.
In this result, column names are inserted at the column’s top. So, in this way, we can add column names with ease.
Example 2
We again import the “pandas as pd” for this new code and create four different lists with the name “lA”, “lB”, “lC”, and “lD” respectively. Then, we add some data to each list. As we add some names “Rao”, “John”, “Smith”, and “Elias” to the first list. The “James”, “Bricks”, “Robert”, and “Leon” is added here to the “lB” list. Now, for the “lC” list, we are adding “12”, “16”, “19”, and “14”. We have the final list, and we have also added some data to this list which are “Birmingham”, “America”, “Canada”, and “Germany”.
After this, we are transferring these lists into the DataFrame, so we put “pd.DataFrame” and named this DataFrame “studentData”. We also print this DataFrame on the terminal using “print ()”. We want to add some column names also to these columns. We utilize the “studentName.columns” method and place the names for each column here. We add “Name”, “Father Name”, “Age”, and “Address” as the names of these columns. And then, print this DataFrame with the column’s name here by using the “print()” statement.
The first DataFrame shows that there is no column name added because we have not added the names on this first DataFrame, but in the second DataFrame, you can notice that there are the names of each column here.
Example 3: Using the set_axis Method
In this example, we are creating five different lists with unique names for each. The names of these lists are “l1”, “l2”, “l3”, “l4”, and “l5” respectively. We put data to all five lists and then converted these lists in DataFrame. We utilize “student” as the name for this DataFrame. We also pass the names of these lists to this DataFrame so it will convert all lists to the DataFrame. Now, we are utilizing another method for adding the column’s name here: the “set_axis ()” method. We add five unique names for all columns and put “axis= 1” and “inplace= True”. This will add the names mentioned here to the top of each column. We print these columns and their names using “print ()”.
We can quickly obtain this output by pressing the run button on this “Spyder” software. Here, you can observe that the name of each column is displayed here as we have added all these names by utilizing the “set_axis ()” method.
Example 4
Now, we are moving to our last example. We have to import the “pandas as pd” and create the lists in the same manner, we explained in the previous codes. We have four different lists here and convert them in the DataFrame. We insert the column’s name using the “DataFrame.columns” method. This method will insert the column names separately to each column. Then, we print this DataFrame which will also print the column names because we have added them here.
After this, we print a line on the screen. We can also change the name of these columns with the help of the “DataFrame.rename” method. When we utilize this method, we can rename the existing column name. We put the existing column name and then place a “:” and then add the new name of the column for renaming this column name. We rename all the column names here using the “student. rename” method. We also put “inplace= True” in this method, and after this, we print them on the terminal by using “print ()”
Here, we add the names of the column first, then replace or rename those names with new names and print columns with both column names. The previous column names we have added are “Name”, “Father Name”, “Age”, and “Address”. We change these names and add new names by utilizing the “DataFrame.rename” method, and the new names here are “Student”, “Student Father”, “Student Age”, and “Location”. So, that means we can add the names of columns and also rename those names.
Conclusion
This guide has covered the methods for adding the column names in great detail and is a straightforward method. The main purpose of creating this guide is to help you understand the concept of “adding column names” in pandas. We have explained two concepts for adding column names here in this guide. These are effortless methods for adding column names separately in “pandas”. We also provide the practical and theoretical explanation of each concept in detail. We hope you will quickly grasp these methods.