Built with Tutify.io

    AIStudyLabs

    Polars Sum Multiple Columns

    Polars Sum Multiple Columns

    In Polars, you can sum multiple columns either row-wise or column-wise using the sum() function along with the select() or with_columns() method, depending on your requirements. In this article, I will explain summing multiple columns in Polars.

    Key Points –

    <a id="h-usage-of-polars-sum-multiple-columns"></a>

    Usage of Polars Sum Multiple Columns

    The sum operation in Polars is used to compute the total of multiple columns either row-wise (horizontally) or column-wise (vertically).

    To run some examples of the Polars sum multiple columns, let’s create a Polars DataFrame.

    
    import polars as pl
    
    studentdetails = {
           "Studentname":["Ram", "Sam", "Scott", "Ann", "John"],
           "Mathematics" :[80,90,85,72,95],
           "Science" :[85,95,80,90,92],
           "English" :[90,85,80,75,95]
                  }
    df = pl.DataFrame(studentdetails)
    print("Original DataFrame:\n", df)
    

    Yields below output.

    You can sum multiple columns row-wise (for each row) using the + operator inside with_columns(). This method is explicit and useful when you want to sum specific columns.

    
    # Sum the subject marks row-wise using the + operator
    df2 = df.with_columns((pl.col("Mathematics") + pl.col("Science") + pl.col("English")).alias("Total Marks"))
    print(df2)
    

    Here,

    <a id="h-sum-columns-row-wise-using-pl-concat-list-function"></a>

    Sum Columns Row-wise Using pl.concat_list() Function

    The pl.concat_list() function in Polars allows us to concatenate multiple column values into a list and then apply functions like sum() to compute the row-wise sum dynamically.

    
    # Sum multiple columns row-wise using pl.concat_list()
    df2 = df.with_columns(pl.concat_list(["Mathematics", "Science", "English"]).list.sum().alias("Total Marks"))
    print(df2)
    

    Here,

    Yields same output as above.

    <a id="h-sum-all-numeric-columns-row-wise"></a>

    Sum All Numeric Columns Row-wise

    When summing all numeric columns row-wise in Polars, we can use the sum_horizontal() function, which dynamically sums all numeric columns without needing to specify them manually.

    
    # Sum all numeric columns row-wise
    df2 = df.with_columns(pl.sum_horizontal(pl.all().exclude("Studentname")).alias("Total Marks"))
    print(df2)
    
    # Sum all numeric columns row-wise
    df2 = df.with_columns(pl.sum_horizontal(pl.exclude("Studentname")).alias("Total Marks"))
    print(df2)
    

    Here,

    Yields same output as above.

    <a id="h-sum-specific-columns-column-wise"></a>

    Sum Specific Columns Column-Wise

    To sum specific columns column-wise (i.e., computing the sum for each selected column) in Polars, you can use the select() method along with pl.col().sum() function.

    
    # Sum specific columns column-wise
    df2 = df.select(pl.col(["Mathematics", "Science"]).sum())
    print(df2)
    
    # Output:
    # shape: (1, 2)
    ┌─────────────┬─────────┐
    │ Mathematics ┆ Science │
    │ ---         ┆ ---     │
    │ i64         ┆ i64     │
    ╞═════════════╪═════════╡
    │ 422         ┆ 442     │
    └─────────────┴─────────┘
    

    Here,

    <a id="h-sum-multiple-columns-column-wise"></a>

    Sum Multiple Columns Column-Wise

    To sum multiple columns column-wise (i.e., computing the sum for each column individually) in Polars, you can use select(pl.all().sum()) or sum() on selected columns.

    
    # Sum multiple columns downward (column-wise)
    df2 = df.select(pl.col(["Mathematics", "Science", "English"]).sum())
    print(df2)
    
    # Sum multiple columns column-wise
    df2 = df.select(pl.all().exclude("Studentname").sum())
    print(df2)
    
    # Output:
    # shape: (1, 3)
    ┌─────────────┬─────────┬─────────┐
    │ Mathematics ┆ Science ┆ English │
    │ ---         ┆ ---     ┆ ---     │
    │ i64         ┆ i64     ┆ i64     │
    ╞═════════════╪═════════╪═════════╡
    │ 422         ┆ 442     ┆ 425     │
    └─────────────┴─────────┴─────────┘
    

    Here,

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

    Conclusion

    In conclusion, polars provides efficient and flexible methods to sum multiple columns, both row-wise and column-wise. Using functions like the + operator, sum_horizontal(), pl.col().sum(), pl.concat_list(), and pl.exclude(), you can dynamically compute sums without the need to manually specify column names.

    Happy Learning!!

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

    Related Articles

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

    References