Pandas dataframe contains an index that is used to identify rows uniquely.
You can convert the index of a pandas dataframe to a column using the df.reset_index(inplace=True) statement.
Basic Example
- The reset index method resets the existing index of the dataframe by making it a new column
- Uses the default index
0,1,...n
as the index.
df.reset_index(inplace=True)
df
Dataframe Will Look Like
The index column is added as a new column of the dataframe, and the default index 0,1,..n
is assigned as the index.
index | First Name | Last Name | Country | Country Code | |
---|---|---|---|---|---|
0 | 0 | Shivam | Pandey | India | 1 |
1 | 1 | Kumar | Ram | US | 2 |
2 | 2 | Felix | John | Germany | 3 |
3 | 3 | Michael | John | India | 1 |
This tutorial explains to you how to convert the index of a pandas dataframe to a column using different methods.
Table of Contents
Creating Dataframe
Use the following code to create a Pandas dataframe.
It contains four columns with data.
No index is assigned explicitly. Hence, pandas will use the default index.
import pandas as pd
users = [ ('Shivam', 'Pandey', 'India', 1),
('Kumar', 'Ram' , 'US', 2 ),
('Felix','John' , 'Germany', 3 ),
('Michael','John' , 'India', 1 ),
]
df = pd.DataFrame( users,
columns = ['First Name' , 'Last Name', 'Country', 'Country Code']
)
df
Dataframe Will Look Like
First Name | Last Name | Country | Country Code | |
---|---|---|---|---|
0 | Shivam | Pandey | India | 1 |
1 | Kumar | Ram | US | 2 |
2 | Felix | John | Germany | 3 |
3 | Michael | John | India | 1 |
Using Reset_index
You can use the reset_index method to convert the index of the pandas dataframe to a column.
The reset index method,
- Converts the existing index to a new column of the dataframe. Uses the name index for the new column
- After converting it, the default index
0,1,2..n
is used as the index - If there are no explicit index, the default index is converted into a column, and again, a default index will be created for the dataframe
inplace=True
parameter is used to denote that the reset operation must be performed in the same dataframe instead of creating a new copy as a result.
Code
df.reset_index(inplace=True)
df
Dataframe Will Look Like
The sample dataframe doesn’t contain any explicit index. It has only the default one. Hence, it is converted as a column, and the default index is again used in the dataframe.
index | First Name | Last Name | Country | Country Code | |
---|---|---|---|---|---|
0 | 0 | Shivam | Pandey | India | 1 |
1 | 1 | Kumar | Ram | US | 2 |
2 | 2 | Felix | John | Germany | 3 |
3 | 3 | Michael | John | India | 1 |
Using Reset_index and New Column Header
This section teaches you how to convert the index of a pandas dataframe to a new column and use a new column header for the new column.
You can
- Convert the index to a new column using the
reset_index()
method. - Use the Pandas rename method to use a new column header for the converted index column.
Code
df.reset_index(inplace=True)
df = df.rename(columns = {'index':'S.No.'})
df
Dataframe Will Look Like
The index column has the new name S.No.
S.No. | First Name | Last Name | Country | Country Code | |
---|---|---|---|---|---|
0 | 0 | Shivam | Pandey | India | 1 |
1 | 1 | Kumar | Ram | US | 2 |
2 | 2 | Felix | John | Germany | 3 |
3 | 3 | Michael | John | India | 1 |
This is how the rename method is used to use a new column header.
Using reset_axis and Reset_index
You can also use the rename_axis() method to rename the index column while converting it into a column.
- It first sets the name for the axis.
- By default, it uses the column axis. Hence, you need not use the axis parameter explicitly.
Code
The following code demonstrates how to rename the column axis.
df.rename_axis('S.No.',inplace=True)
df
Dataframe Will Look Like
The index is renamed as S.No.
First Name | Last Name | Country | Country Code | |
---|---|---|---|---|
S.No. | ||||
0 | Shivam | Pandey | India | 1 |
1 | Kumar | Ram | US | 2 |
2 | Felix | John | Germany | 3 |
3 | Michael | John | India | 1 |
Code
You can use the reset_index()
method to convert the index of a pandas dataframe to a column.
df.reset_index(inplace=True)
df
Dataframe Will Look Like
The index column S.No. is converted to a column and added to the pandas dataframe.
S.No. | First Name | Last Name | Country | Country Code | |
---|---|---|---|---|---|
0 | 0 | Shivam | Pandey | India | 1 |
1 | 1 | Kumar | Ram | US | 2 |
2 | 2 | Felix | John | Germany | 3 |
3 | 3 | Michael | John | India | 1 |
Using Simple Assignment
You can use the assignment operator =
to assign the index values as a new column to the dataframe.
Code
df['index'] = df.index
df
Dataframe Will Look Like
First Name | Last Name | Country | Country Code | index | |
---|---|---|---|---|---|
0 | Shivam | Pandey | India | 1 | 0 |
1 | Kumar | Ram | US | 2 | 1 |
2 | Felix | John | Germany | 3 | 2 |
3 | Michael | John | India | 1 | 3 |
Creating Multiple Index Dataframe
Pandas dataframe can contain multiple and multi-level indexes.
Create a dataframe with multiple indexes, namely ’Code’, ‘ID’.
Code
index_names = pd.MultiIndex.from_tuples([('A', '1'),
('B', '2'),
('C', '3'),
('D', '4')],
names=['Code', 'ID'])
users = [ ('Shivam', 'Pandey', 'India', 1),
('Kumar', 'Ram' , 'US', 2 ),
('Felix','John' , 'Germany', 3 ),
('Michael','John' , 'India', 1 ),
]
df = pd.DataFrame(users, columns = ['First Name' , 'Last Name', 'Country', 'Country Code'], index=index_names)
df
Dataframe Will Look Like
The sample dataframe contains two column indexes.
First Name | Last Name | Country | Country Code | ||
---|---|---|---|---|---|
Code | ID | ||||
A | 1 | Shivam | Pandey | India | 1 |
B | 2 | Kumar | Ram | US | 2 |
C | 3 | Felix | John | Germany | 3 |
D | 4 | Michael | John | India | 1 |
Now you’ll use this dataframe to learn how to convert the multiple indexes to a column name.
Convert Multiindex to Multiple Columns
You can use the reset_index()
method to convert the multiple indexes to multiple columns.
- Converts all the existing indexes into a new column
- Uses the default index as an index.
Code
df.reset_index(inplace=True)
df
Dataframe Will Look Like
Code | ID | First Name | Last Name | Country | Country Code | |
---|---|---|---|---|---|---|
0 | A | 1 | Shivam | Pandey | India | 1 |
1 | B | 2 | Kumar | Ram | US | 2 |
2 | C | 3 | Felix | John | Germany | 3 |
3 | D | 4 | Michael | John | India | 1 |
Convert Specific Indexes of Multiple Indexes into a column
You can use the reset_index()
method to convert the specific indexes into a new column.
To convert a specific index,
- Use the
level
parameter. - Pass the multiple columns as a list to this parameter.
Code
The following code demonstrates how to use the reset_index()
method with the level = ['ID']
parameter to convert the index ID
into a new column.
df.reset_index(inplace=True, level = ['ID'])
df
Dataframe Will Look Like
The id
index is converted into a new column of the dataframe.
ID | First Name | Last Name | Country | Country Code | |
---|---|---|---|---|---|
Code | |||||
A | 1 | Shivam | Pandey | India | 1 |
B | 2 | Kumar | Ram | US | 2 |
C | 3 | Felix | John | Germany | 3 |
D | 4 | Michael | John | India | 1 |