How do I calculate the row sum of specific columns in a Pandas DataFrame?

Susan Sarandon
Release: 2024-11-10 12:51:02
Original
558 people have browsed it

How do I calculate the row sum of specific columns in a Pandas DataFrame?

Row Summation of Given Columns in Pandas DataFrame

In Python's Pandas library, we often encounter the need to calculate the sum of specific columns in a DataFrame. To effectively achieve this, we must consider the appropriate parameters and operations.

Let's consider the following DataFrame:

df = pd.DataFrame({'a': [1, 2, 3],
                   'b': [2, 3, 4],
                   'c': ['dd', 'ee', 'ff'],
                   'd': [5, 9, 1]})
Copy after login

Our objective is to add a column 'e' that represents the sum of columns 'a', 'b', and 'd'. While intuitively, one might approach this with something like:

df['e'] = df[['a', 'b', 'd']].map(sum)
Copy after login

this method fails to produce the desired output.

The correct approach involves utilizing the sum() function with the following parameters:

  • axis=1: Specifies that the summation should be performed along the rows (horizontally).
  • numeric_only=True: Ensures that only numeric columns are considered in the operation, excluding non-numeric columns like 'c'.

Applying this approach yields the following result:

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

Output:

   a  b   c  d   e
0  1  2  dd  5   8
1  2  3  ee  9  14
2  3  4  ff  1   8
Copy after login

Alternatively, if we desire to calculate the sum of only specific columns, we can create a list of those columns and eliminate the ones we don't need using the remove() method.

col_list = list(df)
col_list.remove('d')

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

Output:

   a  b   c  d  e
0  1  2  dd  5  3
1  2  3  ee  9  5
2  3  4  ff  1  7
Copy after login

By utilizing these operations, we can effectively sum rows for specified columns in a Pandas DataFrame, ensuring accurate and efficient data analysis.

The above is the detailed content of How do I calculate the row sum of 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