Built with Tutify.io

    AIStudyLabs

    How to Get Index of Series in Pandas

    How to Get Index of Series in Pandas

    In pandas, Series.index attribute is used to retrieve the index labels associated with 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.

    Key Points –

    <a id="h-quick-examples-of-getting-series-index"></a>

    Quick Examples of Getting Series Index

    If you are in a hurry, below are some quick examples of getting 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']
    

    <a id="h-pandas-series-index-introduction"></a>

    Pandas Series.index Introduction

    Following is the syntax of the Series.index.

    
    # Syntax of series.index
    series.index()
    

    <a id="h-create-pandas-series"></a>

    Create Pandas Series

    A Pandas Series, exclusive to the Pandas library, represents one-dimensional data with index labels. It accommodates various data types, including strings, integers, floats, and other Python objects. Accessing individual elements within the Series is facilitated through their respective default indices.

    Note : The Series data structure shares similarities with the NumPy array data structure, except for one key distinction: while array indices are integers starting from 0, Series indices can encompass a broader range, including strings. These labels need not be unique but must be of a hashable type.

    Now, let’s create a Pandas series,

    
    # Create the Series
    import pandas as pd
    courses = pd.Series(['Java', 'Spark', 'PySpark','Pandas','NumPy', 'Python'])
    print("Create Series:\n",courses)
    

    When you create a series without an index, pandas create a default index with an incremental sequence number starting from 0. This example yields the below output.

    <a id="h-get-the-index-in-pandas-series"></a>

    Get the Index in Pandas Series

    In Series, labels are called indices, and holding the data is called values. If we use Series.index attributes 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.

    <a id="h-get-the-custom-index-in-series"></a>

    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'>
    

    <a id="h-frequently-asked-questions-on-getting-index-of-series"></a>

    Frequently Asked Questions on Getting Index of Series

    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.

    Every pandas series has an index that labels each element. You can access this index using the index attribute.

    <a id="h-conclusion"></a>

    Conclusion

    In conclusion, we have explored the functionality of the pandas.Series.index attribute, which allows us to access the index of a Series in Pandas. We've also discussed the distinctions between default and custom indices, providing a comprehensive understanding of how indices operate within the Series data structure.

    Happy learning !!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References