Through this Python tutorial, We will go through the different methods on How to get index values of Pandas Dataframes. And I will show a few examples of calling different functions to get the indexes of DataFrames in Python.
Moreover, We will also cover the following topics in this Python tutorial:
- Get index values of Pandas DataFrames in different methods
- Get Index values of Pandas DataFrame using the “DataFrame.index” method
- Get Index values of a Pandas DataFrame using the “for loop” method
- Get index values of Pandas DataFrames in different formats
- Get Indexes of a Pandas DataFrames in array format
- Get Indexes of a Pandas DataFrames in list format
- Get indexes of rows with null values in Pandas DataFrame
Get Index values of Pandas DataFrames
The index is an address that will uniquely identify each row in the Pandas DataFrame. Rows indexes are known as Dataframe indexes. We can even extract the index values of a Pandas DataFrame using the existing and most widely used function “DataFrame.Index” in Python. To do this, we need to create a DataFrame first.
Create a DataFrame using Pandas in Python
Here We have created a dictionary of employee data that has the names of the employees, roles, and experiences of the employees in an organization and later it is passed to the “pandas.DataFrame” function in order to convert it to a Pandas DataFrame i.e in the form of rows and columns.
#Importing the necessary libraries
import numpy as np
import pandas as pd
#Create a dictionaryt which has employee names, their experience and roles
data_dict={"Names":["Kelvin", "John", "smith", "Robin","Williams","Nick","Anyy","Messi","Jonas","Xavier"],
"Experience":[13,7,np.nan,9,0,12,21,3,9,17],
"Role": ["IT Analyst","Software Engineer","Software Engineer","Data Analyst","Data Engineer","Data Scientist","ML Engineer","ML Engineer","ML Engineer","Data Scientist"]}
#Create a DataFrame using Pandas
Employee_data=pd.DataFrame(data_dict)
Employee_data
Below is the pandas DataFrame that we have created in Python which has the employee names, their roles, and experiences in which employee Smith’s experience is NaN which means not available.
The numbers that are displayed in the below table or data frame just before the names of employees i.e from 0 to 9 are the indexes of the Pandas DataFrames which identify every row uniquely in Python.
Here we have created a Pandas DataFrame in Python for further analysis. Now we will see the different methods to get the index values of Pandas DataFrames in Python.
Get Index values of Pandas DataFrame using “DataFrame.index” method
In the above code, we have created a table or Pandas DataFrame displaying employees’ background data. We haven’t mentioned the names of the indexes so, by default, it will display the integers and identify every row uniquely in Python.
- By default it returns the Range index which is the start, stop, and step values within the RangeIndex() function.
- The “start” represents the starting index number, “step” represents the skipping difference to the next number, and “stop” represents to stop increment once we reached the last index.
#Printing the index values of the DataFrames
print(Employee_data.index)
The “index” operator in Python fetches the index values of the Pandas DataFrame or dataset. This is how the output for the above code looks like:
This is how we get Index values of Pandas DataFrame using the “DataFrame.index” method in Python.
Get Index Values of Pandas DataFrame Using “for loop” method
Here We are trying to print the values of the indexes of the Pandas DataFrame that are displayed in the Range Index format in the previous code using the “for” loop in Python.
#Printing the index values of a pandas dataframe in python
for index_values in Employee_data.index:
print(index_values)
When we run the loop for Employee_data.index it will return the index values of the data frame. Here, in this example, the indexes are from 0 to 9.
Here we have covered the topic of getting the index values of Pandas DataFrame using the ‘for’ loop.
Get Indexes of a Pandas DataFrames in array format
We can get the indexes of a DataFrame or dataset in the array format using “index.values“. Here, the below code will return the indexes that are from 0 to 9 for the Pandas DataFrame we have created, in an array format.
#Getting Index values of a pandas dataframe in the array format
print(Employee_data.index.values)
If we look at the output image, the indexes of Employee_data are returned in an array.
With the above method in Python, we can get the index values of Pandas DataFrame in array format in Python.
Get Indexes of a Pandas DataFrame in the list format
We can even store the indexes of the Pandas DataFrames in the list format in two ways that are by calling the “list()” function or the “tolist()” function in Python.
Get Indexes of Pandas DataFrame in list format using the “list( )” function
Here We are calling the function “list()” in Python in order to store the indexes of a Pandas DataFrame or dataset in the list format in Python.
#Getting Index values of a Pandas DataFrame in the list format
print(list(Employee_data.index.values))
Here We are storing the indexes of a Pandas DataFrame in the list format using the “list()” function in Python.
This will help us in getting the index values of DataFrame in the list format in Python.
Get Indexes of Pandas DataFrame in list format using “tolist( )” function
Here We are calling the function “tolist()” in Python in order to store the indexes of a DataFrame or dataset in the list format.
#Getting Index values of a Pandas DataFrame in the list format
print(Employee_data.index.values.tolist())
Here We are storing the indexes of the “Employee_data” DataFrame in the list format using the “tolist()” method in Python.
This will help us in getting the index values of Pandas DataFrame in the list format in Python.
Get indexes of rows with null values in Pandas DataFrame
If our dataset has null values then also we can print the indexes of the rows or Pandas DataFrame which has null values in the dataset.
- Here, in the below code data_NaN variable stores, the DataFrame’s information in the true or false format that is by checking whether the row is having any missing value or not.
- If the row has any missing value then it will return True otherwise it will return False in Python Pandas.
- The next line of the code returns the indexes, which have the null values in the rows.
#Printing the indexes with null values in the rows of a Pandas DataFrame in python
data_NaN= Employee_data.isna().any(axis=1)
Employee_data[data_NaN].index.values
Here the below output image shows that 2 is the index number that has some null value in its row in the Employee DataFrame in Python.
This is how we can get the indexes of rows with null values in Pandas DataFrame.
Conclusion
Through this Python pandas tutorial, we have covered topics related to getting indexes of the Pandas DataFrames in different ways like in arrays format, and list format.
And we also covered the topic of getting the indexes of the rows with null values of Pandas DataFrame in Python.
We also saw that mostly in order to get the indexes of the data frames, we are using the inbuilt function that is the “Dataframe.index” attribute to the Pandas DataFrame in Python.
Also, we can see the following Pandas tutorials for a better understanding of Pandas in Python.
- Drop non-numeric columns from pandas DataFrame
- How to get index of rows in Pandas DataFrame
- Percentage Normalization using Crosstab() in Pandas
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.