Open In App

Pandas DataFrame index Property

Last Updated : 05 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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-Property
Pandas DataFrame index Property

Pandas 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:

Screenshot-2024-12-04-190308
Pandas DataFrame index Property

How 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)

Output
Index:
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)

Output
Index 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'])

Output
DataFrame 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)

Output
DataFrame after reset_index:
      Product  Price  Stock
0      Laptop   1000     10
1  Smartphone    800     20
2      Tablet    400     15

Next Article
Practice Tags :

Similar Reads