How to sum values of Pandas dataframe by rows?

How to sum values of Pandas dataframe by rows?

To sum the values of a Pandas DataFrame by rows, you can use the sum method and set the axis parameter to 1. This will sum values along the horizontal axis (i.e., row-wise).

Here's an example:

import pandas as pd # Sample DataFrame data = { 'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9] } df = pd.DataFrame(data) # Compute the sum of values by rows df['Row_Sum'] = df.sum(axis=1) print(df) 

This will output:

 A B C Row_Sum 0 1 4 7 12 1 2 5 8 15 2 3 6 9 18 

In this example, a new column Row_Sum is added to the DataFrame, which contains the sum of values of each row.


More Tags

aws-lambda deployment colors ngrx-effects sqldatareader azure-servicebus-topics immutable.js labview httpwebresponse lamp

More Programming Guides

Other Guides

More Programming Examples