How can I add several columns within a dataframe (broadcasting)?

import numpy as np
import pandas as pd

data = [[30, 19, 6], [12, 23, 14], [8, 18, 20]]

df = pd.DataFrame(data = data, index = ['A', 'B', 'C'], columns = ['Bulgary', 'Robbery', 'Car Theft'])
df

I get the following:

Bulgary Robbery Car Theft
A 30 19 6
B 12 23 14
C 8 18 20

I would like to assign:

df['Total'] = df['Bulgary'] + df['Robbery'] + df['Car Theft']

But does this operation have to be done manually? I am looking for a function that can handle conveniently.

#pseudocode
#df['Total'] = df.Some_Column_Adding_Function([0:3])
#df['Total'] == df['Bulgary'] + df['Robbery'] + df['Car Theft'] returns True
  1. Similarly, how do I add across rows?

>Solution :

Use sum:

df['Total'] = df.sum(axis=1)

Or if you want subset of columns:

df['Total'] = df[df.columns[0:3]].sum(axis=1)
# or df['Total'] = df[['Bulgary', 'Robbery', 'Car Theft']].sum(axis=1)

Leave a Reply