Pandas Series Index() Methods
Last Updated :
11 Jul, 2024
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index.
Pandas Series.index attribute is used to get or set the index labels of the given Series object.
Pandas Series Index() Methods
Syntax: Series.index()
Returns : index
Pandas Series Index Example
Use Series.index attribute to set the index label for the given Series object.
Python
# importing pandas as pd
import pandas as pd
# Creating the Series
series = pd.Series(['New York', 'Chicago', 'Toronto', 'Lisbon'])
# Print the series
print(series)
Output:
0 New York
1 Chicago
2 Toronto
3 Lisbon
dtype: object
Now we will use Series.index attribute to set the index label for the given object.
Python
# Creating the row axis labels
series.index = ['City 1', 'City 2', 'City 3', 'City 4']
# Print the series
print(series)
Output :
City 1 New York
City 2 Chicago
City 3 Toronto
City 4 Lisbon
dtype: object
As we can see in the output, the Series.index attribute has successfully set the index labels for the given Series object.
We can also assign duplicate or nonunique indexes in pandas.
Python
# Creating the row axis labels
series.index = ['City 1', 'City 1', 'City 3', 'City 3']
# Print the series
print(series)
Output:
City 1 New York
City 1 Chicago
City 3 Toronto
City 3 Lisbon
dtype: object
Find element’s index in pandas Series
Use Series.index attribute to get the index labels of the given Series object.
Python
# importing pandas as pd
import pandas as pd
Date = ['1/1/2018', '2/1/2018', '3/1/2018', '4/1/2018']
Index_name = ['Day 1', 'Day 2', 'Day 3', 'Day 4']
# Creating the Series
sr = pd.Series(data = Date, # Series Data
index = Index_name # Index
)
# Print the series
print(sr)
Output :
Day 1 1/1/2018
Day 2 2/1/2018
Day 3 3/1/2018
Day 4 4/1/2018
dtype: object
Now we will use Series.index attribute to get the index label for the given object.
Python
# print the index labels
print(sr.index)
Output :
Index(['Day 1', 'Day 2', 'Day 3', 'Day 4'], dtype='object')
As we can see in the output, the Series.index attribute has successfully returned the index labels for the given Series object.
Pandas Series Index() Methods – FAQs
What is the Index Function in Pandas Series?
The index in a pandas Series is essentially a label or identifier for each row. Every Series in pandas has an index, which you can use to access the elements of the Series. The default index is a sequence of integers starting from 0, but you can also set these indices explicitly to be labels (like strings or datetime objects).
What Are the Methods of Indexing in Pandas?
Pandas provides several methods to index data, including:
- .loc[]: For label-based indexing. This method is used to access a group of rows and columns by labels or a boolean array.
- .iloc[]: For integer position-based indexing. This method is used to access a group of rows and columns by integer positions.
- .at[] and .iat[]: These are similar to
.loc[]
and .iloc[]
but are used to access a single value quickly. - Boolean indexing: Using a boolean vector to access an array. Useful for filtering data.
- Fancy indexing: Using an array of integers to access several columns or rows in particular order.
How to Extract Index from Series in Pandas?
You can access the index of a Series using the .index
attribute:
import pandas as pd
s = pd.Series([1, 2, 3], index=['a', 'b', 'c'])
print(s.index) # Output: Index(['a', 'b', 'c'], dtype='object')
How to Set Index in Pandas Series?
You can set the index of a Series at the time of creation by passing the index as a list, or you can modify the index of an existing Series using the .index
attribute:
# Setting index at creation
s = pd.Series([1, 2, 3], index=['x', 'y', 'z'])
# Modifying index of an existing Series
s.index = ['a', 'b', 'c']
print(s)
How to Give an Index Name in Pandas Series?
You can name an index in a pandas Series using the name
attribute of the index. This is useful for understanding the data, especially when merging or joining datasets:
# Create a Series with an index
s = pd.Series([10, 20, 30], index=pd.Index(['apples', 'oranges', 'bananas'], name='fruits'))
# Access the index name
print(s.index.name) # Output: 'fruits'