Pandas >> How to Rename Column and Index
Table of Contents
In this article, we will talk about how to rename column name or Index in DataFrame
.
Firstly, we prepare data for demonstration.
Preparing data
import pandas as pd
df = pd.DataFrame({
"name": ["Kevin", "Jack", "Mary", "Bob", "Robert", "Amy"],
"score": [80, 90, 95, 93, 88, 81],
"class": ["A", "B", "A", "A", "B", "B"]
}, index=["K", "J", "M", "B", "R", "A"])
df
Result
name | score | class | |
---|---|---|---|
K | Kevin | 80 | A |
J | Jack | 90 | B |
M | Mary | 95 | A |
B | Bob | 93 | A |
R | Robert | 88 | B |
A | Amy | 81 | B |
Rename column names
We can assign a list of new column names to columns
attribute of DataFrame
.
Attention: The column names of original DataFrame will be changed permanently.
df.columns = ["name_1", "score_1", "class_1"]
df
Result
name_1 | score_1 | class_1 | |
---|---|---|---|
K | Kevin | 80 | A |
J | Jack | 90 | B |
M | Mary | 95 | A |
B | Bob | 93 | A |
R | Robert | 88 | B |
A | Amy | 81 | B |
We can also use rename method of DataFrame to change part of column names.
Attention: In this way, the column names of original DataFrame will not be changed and a new DataFrame with new column names will be returned. If you want to change original column names directly, you can specify inplace=True
.
DataFrame.rename(columns={<OLD COLUMN NAME>: <NEW COLUMN NAME>})
# Original DataFrame column names will not be changed
df.rename(columns={"name_1": "name_2", "score_1": "score_2"})
# Original DataFrame column names will be changed
df.rename(columns={"name_1": "name_2", "score_1": "score_2"}, inplace=True)
Result
name_2 | score_2 | class_1 | |
---|---|---|---|
K | Kevin | 80 | A |
J | Jack | 90 | B |
M | Mary | 95 | A |
B | Bob | 93 | A |
R | Robert | 88 | B |
A | Amy | 81 | B |
Rename Index
We can also use rename method to change part or all of Index.
Attention: In this way, the Index of original DataFrame will not be changed and a new DataFrame with new Index will be returned. If you want to change original Index directly, you can specify inplace=True
.
DataFrame.rename(index={<OLD Index NAME>: <NEW Index NAME>})
# Original DataFrame index will not be changed
df.rename(index={"K": "KV", "R": "RB"})
# Original DataFrame index will be changed
df.rename(index={"K": "KV", "R": "RB"}, inplace=True)
Result
name_2 | score_2 | class_1 | |
---|---|---|---|
KV | Kevin | 80 | A |
J | Jack | 90 | B |
M | Mary | 95 | A |
B | Bob | 93 | A |
RB | Robert | 88 | B |
A | Amy | 81 | B |
Resetting Index
We can also use set_index method to reset index of DataFrame.
Attention: In this way, the Index of original DataFrame will not be changed and a new DataFrame with new Index will be returned. If you want to change original Index directly, you can specify inplace=True
.
DataFrame.rename(index={<OLD Index NAME>: <NEW Index NAME>})
# Original DataFrame index will not be changed
df.set_index("name")
# Original DataFrame index will be changed
df.set_index("name", inplace=True)
Result
new index
↓↓↓
name | score | class |
---|---|---|
Kevin | 80 | A |
Jack | 90 | B |
Mary | 95 | A |
Bob | 93 | A |
Robert | 88 | B |
Amy | 81 | B |
If we want to change the current index to a column, we can use reset_index
method. A numeric index will be created as new Index.
# Original DataFrame index will not be changed
df.reset_index()
# Original DataFrame index will be changed
df.reset_index(inplace=True)
Result
new index (ascending numeric index)
↓↓↓
index | name | score | class | |
---|---|---|---|---|
0 | K | Kevin | 80 | A |
1 | J | Jack | 90 | B |
2 | M | Mary | 95 | A |
3 | B | Bob | 93 | A |
4 | R | Robert | 88 | B |
5 | A | Amy | 81 | B |
We can use pandas method chaining (reset_index
().set_index
()) to change index without data loss.
# Original DataFrame index will not be changed
df.reset_index().set_index("name")
Result
new index
↓↓↓
name | index | score | class |
---|---|---|---|
Kevin | K | 80 | A |
Jack | J | 90 | B |
Mary | M | 95 | A |
Bob | B | 93 | A |
Robert | R | 88 | B |
Amy | A | 81 | B |