In this python pandas program, we will rename columns of a given DataFrame using the pandas library.
Steps to solve the program
- Import pandas library as pd.
- Create a dataframe using pd.DataFrame().
- Rename columns of a given DataFrame to A,B,C using df.rename(columns= {‘C1′:’A’,’C2′:’B’,’C3′:’C’}).
- 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