In this article, we will discuss multiple scenarios to get the index column name in pandas DataFrame.
Table of Contents
Preparing Dataset for solution
To quickly get started, let’s create a sample dataframe to experiment. We’ll use the pandas library with some random data.
import pandas as pd import numpy as np # List of Tuples employees= [('Shubham', 'Data Scientist', 'Tech', 5), ('Riti', 'Data Engineer', 'Tech' , 7), ('Shanky', 'Program Manager', 'PMO' , 2), ('Shreya', 'Graphic Designer', 'Design' , 2), ('Aadi', 'Backend Developer', 'Tech', 11), ('Sim', 'Data Engineer', 'Tech', 4)] # Create a DataFrame object from list of tuples df = pd.DataFrame(employees, columns=['Name', 'Designation', 'Team', 'Experience'], index = pd.Series([0, 1, 2, 3, 4, 5], name='Sr_no')) print(df)
Contents of the created dataframe are,
Name Designation Team Experience Sr_no 0 Shubham Data Scientist Tech 5 1 Riti Data Engineer Tech 7 2 Shanky Program Manager PMO 2 3 Shreya Graphic Designer Design 2 4 Aadi Backend Developer Tech 11 5 Sim Data Engineer Tech 4
As observed, the index column name is set as “Sr_no”. Let’s explore how to get this value using multiple methods.
Get Index Column name in DataFrame
Every DataFrame has an index property that can be used to get/set the index details of the DataFrame. To get the index name, we can use the “name” attribute from the index property. Let’s try to code it below.
# getting column name of index print (df.index.name)
Output
Frequently Asked:
- Create a Pandas DataFrame from a Numpy Array
- Drop last N rows of pandas dataframe
- How to get & check data types of Dataframe columns in Python Pandas
- Pandas Tutorial #14 – Sorting DataFrame
Sr_no
Pretty simple, right? We can extract other properties of the index as well using this.
Get index name in MultiIndex DataFrame
Getting the column name of the index in a multi-index DataFrame is slightly different. To understand, let’s first change our existing DataFrame by making both “Name” and “Designation” the DataFrame indexes.
# making the DataFrame multi-index df.set_index(['Name', 'Designation'], inplace=True) print (df)
Output
Team Experience Name Designation Shubham Data Scientist Tech 5 Riti Data Engineer Tech 7 Shanky Program Manager PMO 2 Shreya Graphic Designer Design 2 Aadi Backend Developer Tech 11 Sim Data Engineer Tech 4
As observed, we have multi-index with column names as “Name” and “Designation”. Let’s try to run the above code to extract the index column names.
# getting column name of index print (df.index.name)
Output
None
Much to our surprise, here the output is blank! For the multi-index DataFrames, we slightly need to tweak and use “names” as the attribute to get the list of column names.
# getting column name of index print (df.index.names)
Output
FrozenList(['Name', 'Designation'])
The output here is a FrozenList, which can be easily created to a list or set by using the list function.
The complete example is as follows,
import pandas as pd import numpy as np # List of Tuples employees= [('Shubham', 'Data Scientist', 'Tech', 5), ('Riti', 'Data Engineer', 'Tech' , 7), ('Shanky', 'Program Manager', 'PMO' , 2), ('Shreya', 'Graphic Designer', 'Design' , 2), ('Aadi', 'Backend Developer', 'Tech', 11), ('Sim', 'Data Engineer', 'Tech', 4)] # Create a DataFrame object from list of tuples df = pd.DataFrame(employees, columns=['Name', 'Designation', 'Team', 'Experience'], index = pd.Series([0, 1, 2, 3, 4, 5], name='Sr_no')) print(df) # getting column name of index print (df.index.name) # making the DataFrame multi-index df.set_index(['Name', 'Designation'], inplace=True) print (df) # getting column name of index print (df.index.name) # getting column name of index print (df.index.names)
Output:
Name Designation Team Experience Sr_no 0 Shubham Data Scientist Tech 5 1 Riti Data Engineer Tech 7 2 Shanky Program Manager PMO 2 3 Shreya Graphic Designer Design 2 4 Aadi Backend Developer Tech 11 5 Sim Data Engineer Tech 4 Sr_no Team Experience Name Designation Shubham Data Scientist Tech 5 Riti Data Engineer Tech 7 Shanky Program Manager PMO 2 Shreya Graphic Designer Design 2 Aadi Backend Developer Tech 11 Sim Data Engineer Tech 4 None ['Name', 'Designation']
Summary
In this article, we have discussed multiple scenarios to get the index column name in pandas DataFrame. Thanks.