How to Sum Rows for Specific Columns in a Pandas DataFrame?

Patricia Arquette
Release: 2024-11-13 03:33:02
Original
558 people have browsed it

How to Sum Rows for Specific Columns in a Pandas DataFrame?

How to Sum DataFrame Rows for Specific Columns in Pandas

While working with DataFrames, there may be instances where we need to add a new column that represents the sum of values from multiple existing columns. In this question, the user encounters an issue while attempting to create a new column 'e' that sums the values from columns 'a', 'b', and 'd' in a DataFrame.

The user's initial approach using df[['a', 'b', 'd']].map(sum) was unsuccessful. To correctly sum rows for specific columns in Pandas, we can use the sum() function with the axis parameter set to 1. This signifies that we want to sum the rows rather than the columns.

df['e'] = df.sum(axis=1, numeric_only=True)
Copy after login

In Pandas versions prior to 2.0, we can simply specify axis=1 without numeric_only=True. However, in later versions, non-numeric columns are ignored when numeric_only=True is specified.

If the goal is to sum specific columns, we can create a list of the desired columns and use sum() with axis=1 to calculate the row sums for that subset of columns.

col_list = list(df)
col_list.remove('d')
df['e'] = df[col_list].sum(axis=1)
Copy after login

By following these steps, we can successfully add a new column 'e' that contains the row sums for any combination of numeric columns in a DataFrame.

The above is the detailed content of How to Sum Rows for Specific Columns in a Pandas DataFrame?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template