To sum values of multiple columns row-wise in Python using Pandas, you can use the sum method along with the axis parameter set to 1.
Consider the following example DataFrame:
main.py199 chars8 lines
This creates a DataFrame with four columns - Name, Age, Salary and Expense.
To sum the values of the Salary and Expense columns row-wise and add the result as a new column Total, you can use the following code:
main.py52 chars2 lines
This will create a new column Total with the sum of the Salary and Expense values row-wise.
You can modify this code to sum values of any combination of columns that you want.
main.py63 chars2 lines
This will create a new column New Total with the sum of the Age, Salary, and Expense columns.
gistlibby LogSnag