python
Change the name of columns in a pandas dataframe
You can use df.rename() method to change the column names of the pandas DataFrame. There are more ways to do that - you will see more code examples for the same in this code snippet title.
#By creating a copy - using df.rename() method
df = df.rename(columns={'col_1': 'changed_col_1', 'col_2': 'changed_col_2'})
print(df)
#Without creating copy - using df.rename() method with inplace=True
df.rename(columns={'col_1': 'changed_col_1', 'col_2': 'changed_col_2'}, inplace=True)
print(df)
In the above code snippet, we are using DataFrame.rename() method to change the name of columns. There is a DataFrame df that contains two columns col1 and col2.
In the first example, we are re-assigning our DataFrame to df after changing its column names. To change column names without assigning to DataFrame you can use the inplace=True inside rename() method as shown in the second example.
import pandas as pd
data = [['John', 'New York'], ['Carol', 'Kingdom'], ['Rick', 'Alexendria']]
df = pd.DataFrame(data, columns = ['UserName', 'Address'])
print(df)
# Assigning new dataframe after changing column names
df = df.rename(columns={'UserName': 'FirstName', 'Address': 'MyAddress'})
print(df)
# change columns without making a new copy - use inplace=True
df.rename(columns={'UserName': 'FullName', 'Address': 'FullAddress'}, inplace=True)
print(df)
The rename() method of pandas Dataframe can be used to change or rename its column names.
import pandas as pd
data = [['John', 'New York'], ['Carol', 'Kingdom'], ['Rick', 'Alexendria']]
df = pd.DataFrame(data, columns = ['UserName', 'Address'])
df.columns = ['FirstName', "FullAddress"]
print(df)
You can directly assign column names list to DataFrame.columns property to rename the DataFrame columns. In the code snippet, we are changing UserName to FirstName and Address to FullAddress
import pandas as pd
# create a movie dataframe
data = [['Tony Stark', 'Iron Man'], ['Steve Rogers', 'Captain America']]
movie_df = pd.DataFrame(data, columns = ['Character', 'Movie'])
# using axis = 1
movie_df = movie_df.rename({'Character': 'Name', 'Movie': 'Mervel'}, axis=1)
# using axis = 'columns'
movie_df = movie_df.rename({'Character': 'Name', 'Movie': 'Mervel'}, axis='columns')
# If you do not want to copy and assign new DataFrame use inplace=True
movie_df.rename({'Character': 'Name', 'Movie': 'Mervel'}, axis=1, inplace=True)
Pandas rename() method is very useful to change column names of a DataFrame. You need to provide the name of columns that you want to rename with the new one.
import pandas as pd
# create a movie dataframe
data = [['Tony Stark', 'Iron Man'], ['Steve Rogers', 'Captain America']]
movie_df = pd.DataFrame(data, columns = ['Character', 'Movie'])
# use set_axis and provide list if column names
movie_df.set_axis(['Name', 'Movie Name'], axis=1, inplace=True)
print(movie_df)
set_axis() method of pandas DataFrame can also be used to rename the column names of DataFrame. You need to provide a new column names list also with axis=1 or you can also pass axis='columns'.
Was this helpful?
Similar Posts
- Reorder dataframe columns using column names in pandas
- Create pandas DataFrame and add columns and rows to it
- Delete one or multiple columns from Pandas Dataframe
- Get the count of rows and columns of a Pandas DataFrame
- Sort a DataFrame by rows and columns in Pandas
- Select specific columns from a Pandas DataFrame
- Change column orders using column names list - Pandas Dataframe