Rename DataFrame columns/index names
You can simply rename DataFrame column names using DataFrame.rename() .
DataFrame.rename({'oldName1': 'newName1', 'oldName2': 'newName2'}, axis=1)
Lets create a DataFrame..
import pandas as pd
import numpy as np
df = pd.DataFrame()
df['A'] = [12,13,12]
df['B'] = [45, 40, 48]
df
A B
0 12 45
1 13 40
2 12 48
Rename DataFrame column using DataFrame.rename()
df.rename({'A': 'Age','B':'Marks'}, axis=1, inplace=True)
df

Age Marks
0 12 45
1 13 40
2 12 48
Rename DataFrame index using DataFrame.rename()
df.rename({0: 'One',1:'Two', 2:'Three' }, axis=0, inplace=True)
df
A B
One 12 45
Two 13 40
Three 12 48
Reassign DataFrame column/index names using set_axis()
df.set_axis(['Age', 'Marks'], axis=1, inplace=True)
df
Age Marks
0 12 45
1 13 40
2 12 48
Changing DataFrame Index label using DataFrame.set_axis()
df.set_axis(['Age', 'Marks', 'Grade'], axis=0, inplace=True)
df

A B
Age 12 45
Marks 13 40
Grade 12 48
Related Topics
- Pandas DataFrame: GroupBy Examples
- Pandas DataFrame Aggregation and Grouping
- How to Sort Pandas DataFrame
- Pandas DataFrame: query() function
- Finding and removing duplicate rows in Pandas DataFrame
- How to Replace NaN Values With Zeros in Pandas DataFrame
- How to read CSV File using Pandas DataFrame.read_csv()
- How to Convert Pandas DataFrame to NumPy Array
- How to shuffle a DataFrame rows
- Import multiple csv files into one pandas DataFrame
- Create new column in DataFrame based on the existing columns
- New Pandas dataframe column based on if-else condition
- How to Convert a Dictionary to Pandas DataFrame