Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading