3 Ways to Rename Columns in Pandas DataFrame
For this excercise lets create a dummy dataframe.
Creating the dataframe is very easy. Lets first create a Python dictionary of arrays as shown below.
import pandas as pd
data = {'Name of President':['Barack Obama', 'George Bush', 'Donald Trump'], 'Age of President':[54, 78, 74]}
Once we have the data in above form, we can use the command pd.Dataframe to create our dataframe. Keys will be column names and values will be rows
pd.DataFrame(data)
Ok lets save the dataframe in to a variable now
df = pd.DataFrame(data)
Lets print out the column names
df.columns
Columns can be accessed directly by name df.colname
But since we have spaces in the name of columns, we can't do that. Therefore it is not a good practice to have spaces in the column names. Ofcourse we can access them by another way.
df['Name of President']
How to rename column names in Python Pandas
There are 3 ways of renaming columns in Pandas.
Method 1.
First is just by the position of columns as shown belowdf.columns = ['Name_of_President','Age_of_President']
df.columns
df.head(2)
As we see above, the names have been changed, but this way, you have to do all the columns and also keep in mind the relative postitioning of this column names. We can't do selective column change as shown below.
df.columns = ['Name_of_President']
We got following error...
ValueError: Length mismatch: Expected axis has 2 elements, new values have 1 elements
Better way is by the column name, this way we can choose column.
Method 2.
df.rename(columns={'Age of President':'Age_of_President'})
As we see above, we selected only column "Age of President" to rename. New column has not been saved. We can do that by simply specify inplace=True option as shown below.
df.rename(columns={'Age of President':'Age_of_President'},inplace=True)
df.head()
Lets change the other column name too.
df.rename(columns={'Name of President':'Name_of_President'},inplace=True)
df.head()
Column can now be accessed by the dot notation also
df.Name_of_President
Lets create our original dataframe
df = pd.DataFrame(data)
df.head(1)
How to Rename Column names by lambda Method
Method 3.
If your dataframe is large containing many columns and column names have spaces. Then it will be tedious to rename all the column names one by one. Better way is to use the lambda method
df = pd.DataFrame(data)
df.rename(columns=lambda x: x.replace(" ","_"))
There you go. We achieved the same using the lambda funciton.
Related Notebooks
- How To Drop One Or More Columns In Pandas Dataframe
- Pandas How To Sort Columns And Rows
- Five Ways To Remove Characters From A String In Python
- Select Pandas Dataframe Rows And Columns Using iloc loc and ix
- How To Iterate Over Rows In A Dataframe In Pandas
- Pandas group by multiple custom aggregate function on multiple columns
- How To Convert Python List To Pandas DataFrame
- Convert Pandas DataFrame To Numpy Arrays
- How to Export Pandas DataFrame to a CSV File