Pandas Series.index
attribute is used to get the index labels of the given Series object. A pandas Series holds labeled data and these labels are called indexes, by using these labels we can access series elements and we can do manipulations on our data.
In some situations, we need to get all labels and values separately, using this attribute we can get only index labels. In this article, I will explain how to get index values/labels in pandas Series with examples.
1. Quick Examples of Getting Series Index
If you are in a hurry, below are some quick examples of how to get the pandas series index.
# Quick examples of getting series index
# Example 1 : Create pandas series
courses = pd.Series(['Java', 'Spark', 'PySpark','Pandas','NumPy', 'Python'])
# Example 2 : Get the indices of Series
courses.index = ['Course1', 'Course2', 'Course3', 'Course4', 'Course5', 'Course6']
2. Syntax of Pandas Series.index
Following is the syntax of the Series.index
.
# Syntax of series.index
series.index()
3. Create Pandas Series
Pandas Series is a one-dimensional, Index-labeled data structure that is available only in the Pandas library. It can store all the datatypes such as strings, integer, float, and other python objects. We can access each element in the Series with the help of corresponding default indices.
Note
: Series data structure is same as the NumPy array data structure but only one difference that is arrays indices are integers and starts with 0, whereas in series, the index can be anything even strings. The labels do not need to be unique but they must be of hashable type.
Now, let’s create a series,
# Create the Series
import pandas as pd
courses = pd.Series(['Java', 'Spark', 'PySpark','Pandas','NumPy', 'Python'])
print("Create Series:\n",courses)
Yields below output. When you create a series without an index, pandas create a default index with an incremental sequence number starting from 0.
4. Get the Index in Pandas Series
In Series, labels are called indices, and holding the data is called values. If we use Series.index
attribute we can get the labels. Let’s apply this attribute to the above Series, it will return the indices of the series. When you have a default index it returns the RangeIndex
.
# Get the default indices of Series.
print(courses.index)
print(type(courses.index))
# Output:
# RangeIndex(start=0, stop=6, step=1)
#<class 'pandas.core.indexes.range.RangeIndex'>
From the above, we got the default indices in the form of a range from 0 to 6.
5. Get the Custom Index in Series
We can also create the Series with customized index labels for, that we need to pass the index as a list of values into pd.series()
function.
In this Series, each course name is associated with a custom index label ('Course1'
to 'Course6'
). The dtype: object
indicates that the elements in the Series are of type object, which is the default type for strings in Pandas.
# Create pandas Series
courses = pd.Series(['Java', 'Spark', 'PySpark','Pandas','NumPy', 'Python'], index = ['Course1', 'Course2', 'Course3', 'Course4', 'Course5', 'Course6'])
print(courses)
Yields below output.
# Output:
Course1 Java
Course2 Spark
Course3 PySpark
Course4 Pandas
Course5 NumPy
Course6 Python
dtype: object
As we can see in the output, the Series.index
attribute has successfully set the index labels for the given Series object.
Now, this time we can get the customized indices of the Series individually for, that we need to print only index values.
# Get the custom index
print(courses.index)
print(type(courses.index))
# Output:
# Index(['Course1', 'Course2', 'Course3', 'Course4', 'Course5', 'Course6'], dtype='object')
<class 'pandas.core.indexes.base.Index'>
Frequently Asked Questions on How to Get Index of Series in Pandas
To get the index of a Pandas Series, you can use the index
attribute of the Series. For example, the index
attribute returns an object of type Index
, containing the labels of the Series. The dtype='object'
indicates that the labels are of type object (string labels in this case).
You can retrieve the index of a Pandas Series as a list using the tolist()
method. The tolist()
method converts the index into a Python list, making it easy to work with if you need to manipulate or iterate over the index labels.
It is possible to reset the index of a Pandas Series using the reset_index()
method. This method will reset the index of the Series and convert it into a new DataFrame. The original index will be added as a new column, and a default integer-based index will be assigned to the DataFrame.
To get the index of a specific element in a Pandas Series, you can use the get_loc
method. This method returns the integer location of a particular index label. For example, get_loc('B')
returns the position (index) of the label ‘B’ in the Series. Note that indexing in Python is zero-based, so ‘B’ is at position 1 in the index.
Conclusion
In this article, I have explained the pandas.series.index
attribute is used to get the Series index. also, covered what you get when you have a default index and custom index.
Happy learning !!
Related Articles
- How To Get Value From Pandas Series?
- Convert Pandas Series to String
- How to Sort pandas Series
- Pandas Series.mean() Function
- Pandas Get Floor or Ceil of Series
- How to create Pandas Series in Python?
- Drop Rows From Pandas DataFrame Examples
- Drop Single & Multiple Columns From Pandas DataFrame
- Change the Order of Pandas DataFrame Columns
- Pandas groupby() and sum() With Examples
- Difference Between loc and iloc in Pandas DataFrame
- Find Intersection Between Two Series in Pandas?