Built with Tutify.io

    AIStudyLabs

    Polars DataFrame Drop Nans

    Polars DataFrame Drop Nans

    In polars, the drop_nans() method is used to remove rows from a DataFrame that contain NaN (Not a Number) values. More precisely, dropping NaNs, or nulls, refers to eliminating rows with missing data to ensure a cleaner and more consistent dataset.

    In this article, I will explain the Polars DataFrame drop_nans() function, detailing its syntax, parameters, and how to use it to create a new DataFrame by removing rows that contain NaN values in specified columns.

    Key Points –

    <a id="h-polars-dataframe-drop-nans-introduction"></a>

    Polars DataFrame drop_nans() Introduction

    Let’s know the syntax of the drop_nans() function

    
    # Syntax of polars DataFrame drop_nans()
    DataFrame.drop_nans(
    subset: ColumnNameOrSelector | Collection[ColumnNameOrSelector] | None = None,
    ) → DataFrame
    

    <a id="h-parameters-of-the-polars-dataframe-drop-nans"></a>

    Parameters of the Polars DataFrame drop_nans()

    Following are the parameters of the DataFrame drop_nans() method.

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

    Return Value

    This function returns a new DataFrame with rows containing NaNs dropped based on the subset criteria.

    <a id="h-usage-of-polars-dataframe-drop-nans-method"></a>

    Usage of Polars DataFrame drop_nans() Method

    The drop_nulls() method in Polars removes rows from a DataFrame that contain null (missing) values. By default, it drops rows with nulls in any column, but you can also specify specific columns to check for nulls.

    First, let’s create a Polars DataFrame.

    
    import polars as pl
    
    technologies = {
        'Courses': ["Spark", "PySpark", None, "Python", "Pandas"],
        'Fees': [20000, None, 25000, 30000, None],
    }
    
    df = pl.DataFrame(technologies)
    print("Original DataFrame:\n", df)
    

    Yields below output.

    To remove all rows containing null values in any column, simply use the drop_nulls() method without any arguments. This will return a DataFrame with only the rows that have complete (non-null) data.

    
    # Drop rows with any nulls in any column
    df2 = df.drop_nulls()
    print("DataFrame after dropping rows with any nulls:\n", df2)
    

    Here,

    <a id="h-drop-rows-with-nans-only-in-specific-columns"></a>

    Drop Rows with NaNs only in Specific Columns

    To drop rows that have null (NaN) values in specific columns of a Polars DataFrame, you can use the subset parameter of the drop_nulls() method. Simply provide a list of the column names you want to check, and only rows with nulls in those columns will be removed.

    
    # Drop rows with nulls in specific columns, e.g., 'Courses' only
    df2 = df.drop_nulls(subset=['Courses'])
    print("DataFrame after dropping rows with nulls in 'Courses' column:\n", df2)
    
    # Drop rows with nans only in specific columns
    df2 = df.drop_nulls(["Courses"])
    print("DataFrame after dropping rows with nulls in 'Courses' column:\n", df2)
    
    # Output:
    #  DataFrame after dropping rows with nulls in 'Courses' column:
    # shape: (4, 2)
    ┌─────────┬───────┐
    │ Courses ┆ Fees  │
    │ ---     ┆ ---   │
    │ str     ┆ i64   │
    ╞═════════╪═══════╡
    │ Spark   ┆ 20000 │
    │ PySpark ┆ null  │
    │ Python  ┆ 30000 │
    │ Pandas  ┆ null  │
    └─────────┴───────┘
    

    Here,

    <a id="h-drop-rows-with-nans-in-a-single-column"></a>

    Drop Rows with NaNs in a Single Column

    To remove rows containing null values in a specific column, pass that column name inside a single-item list to the drop_nulls() method.

    
    # Drop rows with nans in a single column
    df2 = df.drop_nulls(["Fees"])
    print("DataFrame after dropping rows with nulls in 'Fees':\n", df2)
    
    # Output:
    # DataFrame after dropping rows with nulls in 'Fees':
    # shape: (3, 2)
    ┌─────────┬───────┐
    │ Courses ┆ Fees  │
    │ ---     ┆ ---   │
    │ str     ┆ i64   │
    ╞═════════╪═══════╡
    │ Spark   ┆ 20000 │
    │ null    ┆ 25000 │
    │ Python  ┆ 30000 │
    └─────────┴───────┘
    

    Here,

    <a id="h-drop-rows-with-nans-using-a-condition"></a>

    Drop Rows with NaNs Using a Condition

    To drop rows with NaNs based on a condition in Polars, use boolean filtering with the filter() method alongside is_not_null() method. This allows you to keep only the rows where the specified values are present.

    
    # Drop rows with nans using a condition
    df2 = df.filter(df["Fees"].is_not_null())
    print("DataFrame after dropping rows where 'Fees' is null:\n", df2)
    
    # Output:
    # DataFrame after dropping rows where 'Fees' is null:
    # shape: (3, 2)
    ┌─────────┬───────┐
    │ Courses ┆ Fees  │
    │ ---     ┆ ---   │
    │ str     ┆ i64   │
    ╞═════════╪═══════╡
    │ Spark   ┆ 20000 │
    │ null    ┆ 25000 │
    │ Python  ┆ 30000 │
    └─────────┴───────┘
    

    Similarly, to drop rows with NaNs (nulls) based on a condition in Polars, you can use the filter() method along with logical expressions like is_null() or is_not_null().

    
    # Custom condition: Drop rows where 'Courses' is null and 'Fees' > 20000
    df2 = df.filter(~(df["Courses"].is_null() & (df["Fees"] > 20000)))
    print("DataFrame after conditional null filtering:\n", df2)
    
    # Output:
    # DataFrame after conditional null filtering:
    # shape: (4, 2)
    ┌────────┬───────┐
    │ Courses│ Fees  │
    │ ---    │ ---   │
    │ str    │ i64   │
    ╞════════╪═══════╡
    │ Spark  │ 20000 │
    │ PySpark│ null  │
    │ Python │ 30000 │
    │ Pandas │ null  │
    └────────┴───────┘
    

    Here,

    <a id="h-drop-rows-with-nans-from-a-dataframe-with-multiple-data-types"></a>

    Drop Rows with NaNs from a DataFrame with Multiple Data Types

    To drop rows with null values from a Polars DataFrame containing multiple data types, you can use methods like drop_nulls() or conditional filtering just as you would with any DataFrame.

    
    import polars as pl
    
    data = {
        "name": ["Alice", None, "Charlie", "David"],
        "age": [25, 30, None, 40],
        "score": [88.5, 92.0, 85.0, None],
        "active": [True, None, True, True],
    }
    df = pl.DataFrame(data)
    
    df2 = df.drop_nulls()
    print("DataFrame after dropping rows with any nulls:\n", df2)
    
    # Output:
    # DataFrame after dropping rows with any nulls:
    # shape: (1, 4)
    ┌───────┬─────┬───────┬────────┐
    │ name  ┆ age ┆ score ┆ active │
    │ ---   ┆ --- ┆ ---   ┆ ---    │
    │ str   ┆ i64 ┆ f64   ┆ bool   │
    ╞═══════╪═════╪═══════╪════════╡
    │ Alice ┆ 25  ┆ 88.5  ┆ true   │
    └───────┴─────┴───────┴────────┘
    

    Here,

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

    Conclusion

    In conclusion, Polars provides efficient and flexible ways to handle and drop null (NaN) values from DataFrames, regardless of data types. Whether you want to remove rows with nulls across all columns or target specific columns, Polars’ methods like drop_nulls() and conditional filtering with filter() make data cleaning simple and effective.

    Happy Learning!!

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

    Related Articles

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

    References