Built with Tutify.io

    AIStudyLabs

    Pandas Get Row Number of DataFrame

    Pandas Get Row Number of DataFrame

    You can get the row number of the Pandas DataFrame using the df.index property. Using this property we can get the row number of a certain value based on a particular column. If you want to get the number of rows you can use the len(df.index) method. In this article, I will explain the row number from the DataFrame with several examples.

    Key Points –

    <a id="h-quick-examples-of-get-row-number-of-dataframe"></a>

    Quick Examples of Get Row Number of DataFrame

    If you are in a hurry, below are some quick examples of how to get row numbers from Pandas DataFrame.

    
    # Quick examples of get row number of dataframe
    
    # Example 1: Get the row number of value based on column
    row_num = df[df['Duration'] == '35days'].index 
    
    # Example 2:  Get the row number using multiple conditions
    row_num = df[(df['Duration'] == '35days') & (df['Courses'] == 'Pandas')].index
    
    # Example 3: Get row number as a numpy array
    row_num = df[df['Discount'] == 1200].index.to_numpy()  
    
    # Example 4: Get row number as a list
    row_num = df[df['Fee'] == 24000]
    print(row_num.index.tolist())
    
    # Example 5: Get maximum row number 
    # Using idxmax() 
    row_num = df['Fee'].idxmax()
    
    # Example 6: Get minimum row number 
    # Using idxmin() 
    row_num = df['Fee'].idxmin()
    

    <a id="h-usage-of-get-row-number-of-pandas-dataframe"></a>

    Usage of Get Row Number of Pandas DataFrame

    To retrieve the row number(s), that is, the index position(s), of rows that meet a specific condition in a Pandas DataFrame, you can use a few different approaches depending on how you define "row number."

    To run some examples of getting the row number of pandas DataFrame, let’s create DataFrame with a Python dictionary of lists.

    
    # Create DataFrame
    import pandas as pd
    import numpy as np
    technologies= {
        'Courses':["Spark","PySpark","Hadoop","Python","Pandas"],
        'Fee'    :[22000,25000,23000,24000,26000],
        'Duration':['30days','50days','35days', '40days','35days'],
        'Discount':[1000,2300,1000,1200,2500]
              }
    df = pd.DataFrame(technologies)
    print("Create DataFrame:\n", df)
    

    Yields below output.

    <a id="h-pandas-get-row-number"></a>

    Pandas Get Row Number

    In order to get the row number from the Pandas DataFrame use the df.index property. For example, I want to get the row number that has a '35days' value in the 'Duration' column. Let's use the property to get the row number from DataFrame based on the condition.

    
    # Get the row number of value based on column
    row_num = df[df['Duration'] == '35days'].index
    print("Get row number of specified value:\n", row_num)
    

    Yields below output.

    Since we have two rows with the same value, it returned the row number for two matched values.

    We can also use multiple conditions to get the row number that matches the value. Let’s see how it can return the row number using multiple conditions.

    
    # Get the row number using multiple condition 
    row_num = df[(df['Duration'] == '35days') & (df['Courses'] == 'Pandas')].index
    print("Get row number of specified value:\n", row_num)
    
    # Output:
    # Get row number of specified value:
    # Int64Index([4], dtype='int64')
    

    <a id="h-get-pandas-row-number-as-numpy-array"></a>

    Get Pandas Row Number as NumPy Array

    Using to_numpy() function along with the property we can get the row number from DataFrame as NumPy Array. The below example gets the row number as a NumPy array.

    
    # Get row number as a NumPy array
    row_num = df[df['Discount'] == 1200].index.to_numpy()  
    print("Get row number of specified value:\n", row_num)
    print(type(row_num))
    
    # Output:
    # Get row number of specified value:
    #  [3]
    #
    

    <a id="h-get-pandas-row-number-as-a-list"></a>

    Get Pandas Row Number as a List

    Using the tolist() function along with the property we can get the row number of a certain value based on a specified column in a DataFrame. This syntax will return the row number as a list.

    
    # Get row number as a list
    row_num = df[df['Fee'] == 24000]
    print(row_num.index.tolist())
    print(type(row_num.index.tolist()))
    
    # Output:
    # [3]
    #
    

    <a id="h-get-maximum-row-number-of-pandas-using-idxmax"></a>

    Get Maximum Row Number of Pandas using idxmax()

    We can also get the maximum row number in a given DataFrame based on a specified column using the idxmax() function. Let's call the idxmax() function with the specified column of the given DataFrame, it will return the maximum row number.

    
    # Get Maximum row number use idxmax() 
    row_num = df['Fee'].idxmax()
    print("Get row number of specified value:\n", row_num)
    
    # Output:
    # Get row number of specified value:
    # 4
    

    <a id="h-get-the-minimum-row-number-using-idxmin"></a>

    Get the Minimum Row Number using idxmin()

    We can also get the minimum row number of a given DataFrame based on a specified column using the idxmin() function. Let's call the idxmin() function with the specified column of the given DataFrame, it will return the minimum row number of the specified column.

    
    # Get Minimum row number use idxmin() 
    row_num = df['Fee'].idxmin()
    print("Get row number of specified value:\n", row_num)
    
    # Output:
    # Get row number of specified value:
    # 0
    

    <a id="h-frequently-asked-questions-on-get-row-number"></a>

    Frequently Asked Questions on Get Row Number

    You can use the index attribute along with the == operator to find the row number where a specific value occurs in a column.

    You can use the isin() method to check if values are present in a column and then use index attribute to get the row numbers.

    You can use the loc method with logical conditions to filter the DataFrame and then get the row numbers.

    You can use the idxmax() or idxmin() functions to get the index of the maximum or minimum value in a column, respectively.

    You can use the isna() method to check for NaN values and then use index to get the row numbers. For example, nan_row_numbers = df[df['ColumnName'].isna()].index.tolist()

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

    Conclusion

    In this article, I have explained how we can get the row number of a certain value based on a particular column from Pandas DataFrame. Also, I explained how to get the row number as a NumPy array and list using to_numpy() and tolist() functions and how to get the max and min row number of a DataFrame.

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

    Related Articles