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'
Similar Reads
Pandas Series Index() Methods
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 ge
4 min read
Pandas Series.fillna() Method
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 fillna() Syntax Pandas Series
3 min read
Pandas Series agg() Method
Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Series.agg() is used to pass a function or list of functions to be applied on a series or even each element of the series separately. In the case of a list of functions, multiple results are returned by the Se
3 min read
Python | Pandas Series.str.isspace() method
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 analysing data much easier. Pandas isspace() is a string method, it checks for All-Space characters in a series an
3 min read
Python | Pandas Index.to_series()
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_series() function create a Series with both index and values equal to
2 min read
Python | Pandas Series.items()
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.items() function iterates ove
2 min read
Creating a Pandas Series
Pandas Series is a one-dimensional labeled array capable of holding data of any type (integer, string, float, python objects, etc.). The axis labels are collectively called index. Labels need not be unique but must be a hashable type. The object supports both integer and label-based indexing and pro
4 min read
Python | Pandas Series.str.index()
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 str.index() method is used to search and return lowest index of a substring in
3 min read
Python | Pandas Series.idxmin()
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.idxmin() function return the
2 min read
Python | Pandas Dataframe/Series.head() method
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 head() method is used to return top n (5 by default) rows of a data frame or se
2 min read