Rename columns of a Pandas DataFrame

dataframe_rename_columns

Skill - Rename columns of a Pandas DataFrame

Table of Contents

Skills Required

Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision

Pandas is a python library.
DataFrame is a data structure provided by the pandas library.

Please go through Pandas DataFrame Basics to learn the basics of pandas DataFrame.

In this post we will learn how to rename the columns of a Pandas Dataframe

The data file used in this post can be downloaded here

Method #1 - ‘rename’ method for targeted columns

This method can be used if we want to rename less number of targeted columns

# import pandas
import pandas as pd
# read csv
df = pd.read_csv("ramen-ratings.csv")

# rename targeted columns using rename method
df.rename(columns={"Review #": "review", "Top Ten":"top_ten"}, inplace=True) 

print(df.head())

Method #2 - assign all column names at once

import pandas as pd
# read csv
df = pd.read_csv("ramen-ratings.csv")

# get dataframe column names as a list
dfCols = df.columns.tolist()
print(dfCols)

# derive new column names from existing column names using a simple list comprehension
# In this case we are stripping off border whitespaces and replacing spaces with _
newCols = [str(x).strip().replace(" ", "_").lower() for  x  in  df.columns]
df.columns = newCols

# assign new column names as a list of strings
# length of new column names list should match the length of original columns list
newCols = ["revw", "brnd", "varty", "stl", "cntry", "strs", "tptn"]
df.columns = newCols

Video

Video for this post can be found here

References


Table of Contents

Comments