Python Pandas program to rename columns of a given DataFrame

In this python pandas program, we will rename columns of a given DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Rename columns of a given DataFrame to A,B,C using df.rename(columns= {‘C1′:’A’,’C2′:’B’,’C3′:’C’}).
  4. Print the output.
				
					import pandas as pd
d = {'C1':[1,3,8],'C2':[6,8,0],'C3':[8,2,6]}
df = pd.DataFrame(d)
print("Old Dataframe: \n",df)
df = df.rename(columns= {'C1':'A','C2':'B','C3':'C'})
print("New DataFrame after renaming columns:")
print(df)
				
			

Output :

				
					Old Dataframe: 
    C1  C2  C3
0   1   6   8
1   3   8   2
2   8   0   6
New DataFrame after renaming columns:
   A  B  C
0  1  6  8
1  3  8  2
2  8  0  6
				
			

get a list of column headers from the DataFrame

change the order of columns in a DataFrame

Leave a Comment