Change language

How to rename columns in Pandas DataFrame

|

About Pandas DataFrame:
Pandas DataFrame — these are rectangular grids that are used to store data. It’s easy to visualize and work with data when stored in a dataFrame.

  • It consists of rows and columns.
  • Each row is a dimension of some instance, while a column is a vector that contains data for some specific attribute / variable.
  • Each data column contains homogeneous data in any specific column, but data rows can contain homogeneous or heterogeneous data in any particular row.
  • Unlike a 2D array, the pandas data axes are labeled.

Method # 1: Using the rename () function.

One way to rename columns in a Pandas dataframe is to use the rename () function. This method is very useful when we need to rename some of the selected columns, because we only need to provide information for the columns that need to be renamed.

Rename one column.

# Import pandas package

import pandas as pd

 
# Define a dictionary containing ICC ratings

rankings = { ’test’ : [ ’ India’ , ’South Africa’ , ’ England’ ,

  ’ New Zealand’ , ’Australia’ ],

’odi’ : [ ’England’ , ’India’ , ’ New Zealand’ ,

’South Africa’ , ’Pakistan’ ],

  ’t20’ : [ ’ Pakistan’ , ’India’ , ’ Australia’ ,

  ’England’ , ’ New Zealand’ ]}

 
# Convert dictionary to DataFrame

rankings_pd = pd.DataFrame (rankings)

 
# Before renaming columns

print (rankings_pd)

 

rankings_pd.rename (columns = { ’test’ : ’ TEST’ }, inplace = True )

  
# After renaming columns

print ( "After modifying first column:" , rankings_pd.columns)

Output:

Rename multiple columns.

# Import pandas package

import pandas as pd

 
# Define a dictionary containing ICC ratings

rankings = { ’ test’ : [ ’India’ , ’ South Africa’ , ’England’ ,

’New Zealand’ , ’Australia’ ],

  ’ odi’ : [ ’England’ , ’India’ , ’ New Zealand’ ,

’South Africa’ , ’ Pakistan’ ],

’ t20’ : [ ’Pakistan’ , ’ India’ , ’Australia’ ,

’England’ , ’ New Zealand ’ ]}

  
# Convert dictionary to DataFrame

rankings_pd = pd.DataFrame (rankings)

 
# Before renaming columns

print (rankings_pd.columns)

  

rankings_pd.rename (columns = { ’ test’ : ’TEST’ , ’odi’ : ’ODI’ ,

  ’ t20 ’ : ’ T20’ }, inplace = True )

 
# After renaming columns

print (rankings_pd.columns)

Exit:

Method # 2: assigning a list of new column names

Columns can also be renamed by assign by directly assigning the list containing the new names to the columns attribute of the dataframe object for which we want to rename the columns. The disadvantage of this method is that we need to provide new names for all columns, even if you only want to rename some of the columns.

# Pandas package import

import pandas as pd

  
# Define a dictionary containing ICC ratings

rankings = { ’test’ : [ ’India’ , ’ South Africa’ , ’England’ ,

’New Zealand’ , ’Australia’ ],

’odi’ : [ ’ England’ , ’India’ , ’ New Zealand’ ,

’South Africa’ , ’ Pakistan’ ],

’t20’ : [ ’Pakistan’ , ’ India’ , ’Australia’ ,

  ’England’ , ’New Zealand’ ]}

  
# Convert dictionary to DataFrame

rankings_pd = pd.DataFrame (rankings)

 
# Before renaming columns

print (rankings_pd.columns)

 

rankings_pd.columns = [ ’TEST’ , ’ODI’ , ’ T-20’ ]

 
# After renaming columns

print (rankings_pd.columns)

Output:

How to rename columns in Pandas DataFrame Python functions: Questions

How to rename columns in Pandas DataFrame rename: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method