Built with Tutify.io

    AIStudyLabs

    Pandas - Retrieve Number of Rows From DataFrame

    Pandas - Retrieve Number of Rows From DataFrame

    To retrieve the number of rows from pandas DataFrame using either len(), axes(), shape() and info() methods. In this article, I will explain how to retrieve the number of rows from pandas DataFrame with examples.

    Key Points –

    <a id="h-quick-examples-of-retrieve-number-rows-from-dataframe"></a>

    Quick Examples of Retrieve Number Rows From DataFrame

    If you are in a hurry, below are some quick examples of how to retrieve number rows from DataFrame.

    
    # Quick examples of retrieve number rows from dataframe
    
    # Using df.axes() method 
    # To get number rows
    rows_count = len(df.axes[0])
    
    # Using DataFrame.len() method 
    df2 = len(df)
    
    # Return number of rows 
    # Using df.len()
    df2 = len(df.index)
    
    # Using DataFrame.shape method
    df2 = df.shape[0]
    
    # Get the number of columns and rows
    df2 = df.shape
    
    # Using DataFrame.info() method 
    df.info()
    

    Now, let's create a DataFrame with a few rows and columns, execute these examples and validate results. Our DataFrame contains column names Courses, Fee, Duration, and Discount.

    
    # Create DataFrame
    import pandas as pd
    technologies = {
        'Courses':["Spark","PySpark","Python","pandas","Hadoop"],
        'Fee' :[20000,25000,22000,24000,30000],
        'Duration':['30days','40days','35days','60days','50days'],
        'Discount':[1000,2300,2500,2000,3000]
                  }
    index_labels=['r1','r2','r3','r4','r5']
    df = pd.DataFrame(technologies,index=index_labels)
    print(df)
    

    Yields below output.

    
    # Output:
        Courses    Fee Duration  Discount
    r1    Spark  20000   30days      1000
    r2  PySpark  25000   40days      2300
    r3   Python  22000   35days      2500
    r4   pandas  24000   60days      2000
    r5   Hadoop  30000   50days      3000
    

    <a id="h-using-dataframe-axes-method-to-retrieve-number-rows"></a>

    Using DataFrame.axes() Method to Retrieve Number Rows

    Use pandas.DataFrame.axes() method to retrieve the number of rows (count of rows). It accepts the argument '1' for columns and '0' for rows. For instance, len(df.axes[0]) to returns the number of rows.

    
    # Using df.axes() method to get number rows
    rows = len(df.axes[0])
    df2 = str(rows)
    print("Get number of Rows: " + df2)
    

    Yields below output.

    
    # Output:
    Get number of Rows: 5
    

    Yields same output as above.

    <a id="h-using-dataframe-len-method-to-retrieve-number-rows"></a>

    Using DataFrame.len() Method to Retrieve Number Rows

    You can also use DataFrame.len() method to retrieve a list of the rows in DataFrame. For example, len(df.index) returns the count of rows.

    
    # Using DataFrame.len() method 
    df2 = len(df)
    print("Get number of rows:", df2)
    
    # Return number of rows using df.len()
    df2 = len(df.index)
    print("Get number of rows:", df2)
    

    Yields the same output as above.

    <a id="h-retrieve-number-rows-to-using-dataframe-shape-method"></a>

    Retrieve Number Rows to Using DataFrame.shape() Method

    Use DataFrame.shape to get the number of columns and rows as a shape of the DataFrame, which is a tuple where the shape[0] element is a number of rows and shape[1] is the number of columns. For example df.shape[0] gets the number of rows.

    
    # Using DataFrame.shape method
    df2 = df.shape[0]
    print("Get number of rows:", df2)
    
    # Get the number of columns and rows
    df2 = df.shape
    print(df2)
    

    Yields the same output as above.

    <a id="h-using-dataframe-info-method"></a>

    Using DataFrame.info() Method

    DataFrame.info() method provides information about the DataFrame including dtype of columns and index, memory usage, number of columns, etc.

    
    # Using DataFrame.info() method 
    df.info()
    

    Yields below output.

    
    # Output:
    <class 'pandas.core.frame.DataFrame'>
    Index: 5 entries, r1 to r5
    Data columns (total 4 columns):
     #   Column    Non-Null Count  Dtype 
    ---  ------    --------------  ----- 
     0   Courses   5 non-null      object
     1   Fee       5 non-null      int64 
     2   Duration  5 non-null      object
     3   Discount  5 non-null      int64 
    dtypes: int64(2), object(2)
    memory usage: 200.0+ bytes
    

    <a id="h-complete-example-for-retrieve-number-rows-from-dataframe"></a>

    Complete Example For Retrieve Number Rows From DataFrame

    
    import pandas as pd
    
    technologies = {
        'Courses':["Spark","PySpark","Python","pandas","Hadoop"],
        'Fee' :[20000,25000,22000,24000,30000],
        'Duration':['30days','40days','35days','60days','50days'],
        'Discount':[1000,2300,2500,2000,3000]
                  }
    index_labels=['r1','r2','r3','r4','r5']
    df = pd.DataFrame(technologies,index=index_labels)
    print(df)
    
    # Using df.axes() method to get number rows
    rows = len(df.axes[0])
    df2 = str(rows)
    print("Get number of Rows: " + df2)
    
    # Using DataFrame.len() method 
    df2 = len(df)
    print("Get number of rows:", df2)
    
    # Return number of rows using df.len()
    df2 = len(df.index)
    print("Get number of rows:", df2)
    
    # Using DataFrame.shape method
    df2 = df.shape[0]
    print("Get number of rows:", df2)
    
    # Get the number of columns and rows
    df2 = df.shape
    print(df2)
    
    # Using DataFrame.info() method 
    df.info()
    

    <a id="h-faq-on-pandas-retrieve-number-of-rows-from-dataframe"></a>

    FAQ on Pandas Retrieve Number of Rows From DataFrame

    To get the number of rows in a DataFrame, use the shape attribute. The shape attribute returns a tuple where the first value is the number of rows and the second is the number of columns.

    The count() method counts non-null values in each column, so it’s generally not recommended to use count() for getting the total number of rows.

    To retrieve the number of rows when the DataFrame is empty, you can simply use the shape[0] attribute or len(df). Both methods will return 0 when the DataFrame is empty.

    To count rows based on a condition, use a boolean mask with the len() function or the shape attribute.

    You can get both the number of rows and columns in a Pandas DataFrame using the shape attribute.

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

    Conclusion

    In this article, you have learned how to retrieve the number of rows from pandas DataFrame using DataFrame.axes(), DataFrame.len(),DataFrame.shape() and DataFrame.info() method with examples.

    Happy Learning !!

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

    Related Articles

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

    References