Pandas DataFrame index Property
Last Updated :
23 Jul, 2025
In Pandas we have names to identify columns but for identifying rows, we have indices. The index property in a pandas dataFrame allows to identify and access specific rows within dataset. Essentially, the index is a series of labels that uniquely identify each row in the DataFrame. These labels can be integers, strings, or any other hashable type, making it versatile for various data manipulation tasks. To access the index or change the index values, we use .index method. Let us consider a sample example:
Python
import pandas as pd
data = {'Name': ['John', 'Ram', 'Sita'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
print(df.index)
Output:
Pandas DataFrame index PropertyPandas DataFrame Index Property: Syntax and Parameters
Index is an array like structure that is immutable and hashable, with Syntax : dataframe.index.There are different categories of Index:
- RangeIndex: Generates range of integer values starting from 0.
- Int64Index: These are basically 64-bit integer labels.
- Float64Index: These are 64-bit float labels that are used as index for the rows.
- DateTimeIndex: As the name suggests, Date-time value is the index of the dataframe.
- Multi-index: These are used for Multi-index dataframes.
Below is the sample example that illustrates the use of each index types.
Python
import pandas as pd
data = {
'Name': ['Alice', 'Bob', 'Charlie'],'Age': [25, 30, 35],'Score': [85, 90, 88]}
df = pd.DataFrame(data)
# 1. RangeIndex (Default)
print("1. RangeIndex (Default):")
print(df, "\n")
# 2. Int64Index
df.index = [101, 102, 103]
print("2. Int64Index:")
print(df, "\n")
# 3. Float64Index
df.index = [1.1, 2.2, 3.3]
print("3. Float64Index:")
print(df, "\n")
# 4. DatetimeIndex
df.index = pd.date_range(start='2024-01-01', periods=3, freq='D')
print("4. DatetimeIndex:")
print(df, "\n")
# 5. MultiIndex
df.index = pd.MultiIndex.from_tuples(
[('Group1', 'A'), ('Group1', 'B'), ('Group2', 'A')],
names=['Group', 'Subgroup']
)
print("5. MultiIndex:")
print(df, "\n")
Output:
Pandas DataFrame index PropertyHow to Access the Index?
By default, in dataframes rangeindex is the default index generated. Now to access the index values we use .index to get the list of index values. For further details with respect to the indices we use .index.name and .index.values. Let us consider a sample example.
Python
import pandas as pd
data = {'Product': ['Laptop', 'Smartphone', 'Tablet'],'Price': [1000, 800, 400],'Stock': [10, 20, 15]}
df = pd.DataFrame(data)
# Setting a custom index with a name
df.index = ['P001', 'P002', 'P003']
df.index.name = 'ProductID'
# Accessing the index
print("Index:")
print(df.index)
# Accessing the name of the index
print("\nIndex Name:")
print(df.index.name)
# Accessing the values of the index
print("\nIndex Values:")
print(df.index.values)
OutputIndex:
Index(['P001', 'P002', 'P003'], dtype='object', name='ProductID')
Index Name:
ProductID
Index Values:
['P001' 'P002' 'P003']
From the output we can see that using .index, .index.names and .index.values we can get detailed information about the index column like data type, name and values.
Can we modify the index of the dataframe?
Pandas provides us with many functionalities to explore the properties of indices. One such method is customizing the index. Using set_index() we can customize and set the index value of the dataframe. Let us consider a dataframe.
Python
import pandas as pd
data = {
'City': ['New York', 'Los Angeles', 'Chicago'],'Population': [8419600, 3980400, 2716000],
'Area': [783.8, 503, 589]
}
df = pd.DataFrame(data)
# Setting 'City' column as the index
df.set_index('City', inplace=True)
# Accessing the index
print("\nIndex after setting:")
print(df.index)
print(df)
OutputIndex after setting:
Index(['New York', 'Los Angeles', 'Chicago'], dtype='object', name='City')
Population Area
City
New York 8419600 783.8
Los Angele...
In this we can see that using the set_index method, we can set any column as our index column. In this way we can customize the index column.
How to Use loc for Label-Based Indexing?
Since index of the dataframe is nothing but label, we can use the loc to access row or set of rows from the dataframe. We can use slicing in the loc as well to access the subset of rows from the dataframe. Let us consider one example. In this sample dataframe, we have customized the index and using loc we are accessing a set of rows from the dataframe.
Python
import pandas as pd
data = {'Product': ['Laptop', 'Smartphone', 'Tablet'],'Price': [1000, 800, 400],}
df = pd.DataFrame(data)
# Modify the index by setting the 'Product' column as the index
df.set_index('Product', inplace=True)
# Display the DataFrame with 'Product' as the index
print("DataFrame with 'Product' as the index:")
print(df.loc['Laptop':'Smartphone'])
OutputDataFrame with 'Product' as the index:
Price
Product
Laptop 1000
Smartphone 800
From the output we can see that we have fetched some rows from the dataset. Using loc we can filter out our data just by passing the index values. Here it is to be noted that loc considers the last value.
How to Reset an Index?
When we group the data based on multiple columns or use hierarchical indexing, it becomes complex for further operations as there are presence of more than one index columns. So to tackle this situation, we can reset our index using reset_index. Below is the sample example that illustrates the useof reset_index. From the code we can see that we have reset our index using this method with inplace=True. This ensures that the changes are reflected in the original dataframe as well.
Python
import pandas as pd
# Sample DataFrame with custom index
data = {'Product': ['Laptop', 'Smartphone', 'Tablet'],'Price': [1000, 800, 400],'Stock': [10, 20, 15]}
df = pd.DataFrame(data)
# Setting 'Product' as the index
df.set_index('Product', inplace=True)
# Reset the index, keeping the old index as a column
df_reset = df.reset_index(drop=False)
print("\nDataFrame after reset_index:")
print(df_reset)
OutputDataFrame after reset_index:
Product Price Stock
0 Laptop 1000 10
1 Smartphone 800 20
2 Tablet 400 15
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
What is an Operating System? An Operating System is a System software that manages all the resources of the computing device. Acts as an interface between the software and different parts of the computer or the computer hardware. Manages the overall resources and operations of the computer. Controls and monitors the execution o
5 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
Python Functions Python Functions is a block of statements that does a specific task. The idea is to put some commonly or repeatedly done task together and make a function so that instead of writing the same code again and again for different inputs, we can do the function calls to reuse code contained in it over an
9 min read