Pandas DataFrame index Property
Last Updated :
05 Dec, 2024
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
Pandas DataFrame.reset_index() The reset_index() method in Pandas is used to manage and reset the index of a DataFrame. It is useful after performing operations that modify the index such as filtering, grouping or setting a custom index. By default reset_index() reverts to a clean, default integer-based index (0, 1, 2, ...) which
4 min read
Reset Index in Pandas Dataframe Letâs discuss how to reset the index in Pandas DataFrame. Often We start with a huge data frame in Pandas and after manipulating/filtering the data frame, we end up with a much smaller data frame. When we look at the smaller data frame, it might still carry the row index of the original data frame.
6 min read
Python | Pandas DataFrame.set_index() Pandas set_index() method is used to set one or more columns of a DataFrame as the index. This is useful when we need to modify or add new indices to our data as it enhances data retrieval, indexing and merging tasks. Setting the index is helpful for organizing the data more efficiently, especially
3 min read
Pandas Dataframe Index Index in pandas dataframe act as reference for each row in dataset. It can be numeric or based on specific column values. The default index is usually a RangeIndex starting from 0, but you can customize it for better data understanding. You can easily access the current index of a dataframe using th
3 min read
Python | Pandas Index.to_frame() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.to_frame() function create a dataFrame from the given index with a column
2 min read
Python | Pandas dataframe.eq() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas dataframe.eq() is a wrapper used for the flexible comparison. It provides a con
3 min read