Pandas Series Index Attribute
Last Updated :
26 Mar, 2025
Pandas Series is a one-dimensional labeled array capable of holding any data type (integers, strings, floats, etc.), with each element having an associated label known as its index. The Series.index attribute in Pandas allows users to get or set the index labels of a Series object, enhancing data accessibility and retrieval efficiency. Example:
Python
import pandas as pd
data = pd.Series([10, 20, 30, 40], index=['a', 'b', 'c', 'd'])
# Accessing the index
print("Original Index:", data.index)
# Modifying the index
data.index = ['w', 'x', 'y', 'z']
print("Modified Series:\n", data)
OutputOriginal Index: Index(['a', 'b', 'c', 'd'], dtype='object')
Modified Series:
w 10
x 20
y 30
z 40
dtype: int64
Explanation: This code creates a Pandas Series with custom index labels ('a', 'b', 'c', 'd') and retrieves the index using data.index. It then updates the index to ('w', 'x', 'y', 'z').
Syntax
Series.index # Access index labels
Series.index = new_index # Modify index labels
Parameter: This method does not take any parameter.
Returns: Index labels of the Series.
Functionality:
- Retrieves the current index labels of the Series.
- Can be used to set new index labels.
- Supports both unique and duplicate index labels.
- Useful for locating elements efficiently within a Series.
Examples of Pandas Series Index() Attribute
Example 1. Assigning Duplicate Index Labels
Pandas allows assigning duplicate index labels, which can be useful in cases where multiple elements share the same category.
Python
import pandas as pd
series = pd.Series(['New York', 'Chicago', 'Toronto', 'Lisbon'])
# Creating the row axis labels
series.index = ['City 1', 'City 1', 'City 3', 'City 3']
print(series)
OutputCity 1 New York
City 1 Chicago
City 3 Toronto
City 3 Lisbon
dtype: object
Explanation: Even with duplicate labels ('City 1' and 'City 3' appearing twice), Pandas maintains the Series structure and ensures data integrity.
Example 2. Retrieving Index Labels
The Series.index attribute can also be used to retrieve the current index labels of a Series.
Python
import pandas as pd
Date = ['1/1/2018', '2/1/2018', '3/1/2018', '4/1/2018']
idx_name = ['Day 1', 'Day 2', 'Day 3', 'Day 4']
sr = pd.Series(data = Date,index = idx_name)
print(sr.index)
OutputIndex(['Day 1', 'Day 2', 'Day 3', 'Day 4'], dtype='object')
Explanation: The index labels ('Day 1' to 'Day 4') are assigned to a Series and retrieved using series.index.
Example 3. Resetting Index to Default
If needed, we can reset the index to default integer values.
Python
import pandas as pd
Date = ['1/1/2018', '2/1/2018', '3/1/2018', '4/1/2018']
idx_name = ['Day 1', 'Day 2', 'Day 3', 'Day 4']
sr = pd.Series(data = Date, # Series Data
index = idx_name # Index
)
# Resetting index to default
sr.reset_index(drop=True, inplace=True)
print(sr)
Output0 1/1/2018
1 2/1/2018
2 3/1/2018
3 4/1/2018
dtype: object
Explanation: reset_index(drop=True, inplace=True) removes the custom index and replaces it with the default integer index while modifying the Series in place.