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 hurry below are some quick examples of how to get the pandas series index.
# Below are the quick examples # 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
# 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(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.
# Output: 0 Java 1 Spark 2 PySpark 3 Pandas 4 NumPy 5 Python dtype: object
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
# 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
# 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'>
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 !!
- How to create Pandas Series in Python?
- How to Sort pandas Series
- 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