Built with Tutify.io

    AIStudyLabs

    Polars DataFrame height – Explained by Examples

    Polars DataFrame height – Explained by Examples

    In Polars, you can get the number of rows (height) of a DataFrame using the height attribute. The height of a DataFrame refers to the number of rows it contains. It is a quick way to check the size of the DataFrame in terms of rows.

    In this article, I will explain the Polars DataFrame height property, including its syntax, parameters, and usage. Through detailed examples, I will show how it returns an integer representing the total number of rows in a DataFrame.

    Key Points –

    <a id="h-syntax-of-polars-dataframe-height-property"></a>

    Syntax of Polars DataFrame height Property

    Following is the syntax of the DataFrame height attribute.

    
    # Syntax of height
    property DataFrame.height: int
    

    <a id="h-return-value"></a>

    Return Value

    This function returns an integer representing the number of rows in the DataFrame.

    <a id="h-usage-of-polars-dataframe-height-attribute"></a>

    Usage of Polars DataFrame height Attribute

    The height property in Polars is used to get the number of rows in a DataFrame. It is an efficient and direct way to check the size of your dataset.

    Now, let’s create a Polars DataFrame.

    
    import polars as pl
    
    technologies= {
        'Courses':["Spark","PySpark","Hadoop","Python","Pandas","PySpark","Java"],
        'Fee' :[22000, 25000, 23000, 24000, 26000, 30000, 35000],
        'Discount':[1000, 2300, 1000, 1200, 2500, 2000, 2200],
        'Duration':['35days','40days','65days','50days','60days','30days','45days']
              }
    
    df = pl.DataFrame(technologies)
    print("Original DataFrame:\n", df)
    

    Yields below output.

    You can use the height property to get the number of rows in a Polars DataFrame.

    
    # Get the number of rows
    print("Number of rows in the DataFrame:", df.height)
    

    Here,

    Alternatively, you can use the shape property, which returns both rows and columns as a tuple.

    
    df2 = df.shape[0]
    print("Number of rows:", df2)
    
    # Output:
    # Number of rows: 7
    

    <a id="h-conditional-execution-based-on-row-count"></a>

    Conditional Execution Based on Row Count

    The height attribute in Polars enables you to execute code conditionally based on the number of rows in a DataFrame. This is useful for handling large datasets, enforcing business rules, or performing validations by applying different logic depending on the DataFrame’s size.

    
    # Conditional execution based on row count
    if df.height == 0:
        print("The DataFrame is empty.")
    elif df.height < 5:
        print("The DataFrame has a small number of rows:", df.height)
    else:
        print("The DataFrame has a large number of rows:", df.height)
        
    # Output:
    # The DataFrame has a large number of rows: 7
    

    <a id="h-filtering-data-and-checking-new-row-count"></a>

    Filtering Data and Checking New Row Count

    You can filter a Polars DataFrame based on conditions and then use the height attribute to check the number of rows in the filtered DataFrame. This helps determine how many rows remain after applying the filter.

    
    # Filtering: Select courses with Fee greater than 25,000
    filtered_df = df.filter(df["Fee"] > 25000)
    
    # Checking the number of rows after filtering
    print("Number of rows before filtering:", df.height)
    print("Number of rows after filtering:", filtered_df.height)
    
    # Output:
    # Number of rows before filtering: 7
    # Number of rows after filtering: 3
    

    Here,

    <a id="h-checking-if-a-dataframe-is-empty"></a>

    Checking If a DataFrame is Empty

    You can determine if a DataFrame is empty (i.e., has zero rows) using the df.height property. If df.height returns 0, it indicates that the DataFrame contains no rows.

    
    import polars as pl
    
    # Creating an empty Polars DataFrame
    df = pl.DataFrame({})
    
    # Check if the DataFrame is empty
    if df.height == 0:
        print("The DataFrame is empty!")
    else:
        print("The DataFrame has data.")
    
    # Output:
    # The DataFrame is empty!
    

    Here,

    <a id="h-comparing-two-dataframes-by-height"></a>

    Comparing Two DataFrames by Height

    We can compare the number of rows (height) of two DataFrames using the df.height property. This is useful when analyzing datasets of different sizes.

    
    import polars as pl
    
    # Creating two Polars DataFrames
    df1 = pl.DataFrame({
        "A": [1, 3, 5]
    })
    
    df2 = pl.DataFrame({
        "A": [2, 4, 6, 8, 10]
    })
    
    # Comparing DataFrame heights
    if df1.height < df2.height:
        print("df2 has more rows than df1")
    else:
        print("df1 has more or equal rows as df2")
    
    # Output:
    # df2 has more rows than df1
    

    Here,

    <a id="h-polars-dataframe-height-attribute-with-missing-values"></a>

    Polars DataFrame height Attribute with Missing Values

    The height attribute returns the number of rows in a DataFrame, regardless of missing values (nulls). Even if some values are missing, the height remains the total row count.

    
    import polars as pl
    
    technologies= {
        'Courses':["Spark","PySpark",None,"Python","Pandas","PySpark","Java"],
        'Fee' :[22000, 25000, None, 24000, 26000, 30000, None],
        'Discount':[1000, 2300, 1000, 1200, None, 2000, 2200],
        'Duration':['35days',None,'65days','50days','60days',None,'45days']
              }
    
    df = pl.DataFrame(technologies)
    
    # Get the number of rows
    print("Total number of rows:", df.height)
    
    # Output:
    # Total number of rows: 7
    

    <a id="h-counting-only-non-missing-rows-in-a-column"></a>

    Counting Only Non-Missing Rows in a Column

    You can count the number of non-missing (non-null) rows in a specific column using drop_nulls() or filter(). This is useful for checking data completeness before analysis. We can use drop_nulls() on a specific column and then check the height.

    
    # Count non-null rows in the "Courses" column
    df2 = df.drop_nulls(subset=["Courses"]).height
    print("Rows where 'Courses' is not null:", df2)
    
    # Output:
    # Rows where 'Courses' is not null: 6
    

    <a id="h-filtering-out-rows-with-missing-values"></a>

    Filtering Out Rows with Missing Values

    You can remove rows with missing (null) values using drop_nulls(). This is useful when you need a clean dataset for analysis.

    
    # Remove rows with any missing values
    df_cleaned = df.drop_nulls()
    
    # Print cleaned DataFrame
    print(df_cleaned)
    print("Rows after dropping missing values:", df_cleaned.height)
    
    # Output:
    # shape: (2, 4)
    ┌─────────┬───────┬──────────┬──────────┐
    │ Courses ┆ Fee   ┆ Discount ┆ Duration │
    │ ---     ┆ ---   ┆ ---      ┆ ---      │
    │ str     ┆ i64   ┆ i64      ┆ str      │
    ╞═════════╪═══════╪══════════╪══════════╡
    │ Spark   ┆ 22000 ┆ 1000     ┆ 35days   │
    │ Python  ┆ 24000 ┆ 1200     ┆ 50days   │
    └─────────┴───────┴──────────┴──────────┘
    # Rows after dropping missing values: 2
    

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

    Conclusion

    In conclusion, the height attribute in Polars provides a quick and efficient way to determine the number of rows in a DataFrame. It is useful for conditionally executing code, checking if a DataFrame is empty, and analyzing the size of filtered data.

    Happy Learning!!

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

    Related Articles

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

    References