Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Big Data Hadoop
  3. Question
Big Data Hadoop

Pandas: sum up multiple columns into one column without last column

Asked by Cameron Oliver Jun 24, 2021 1.5K views 1 answer
Share

About this question

If I have a data frame similar to this one

Apples   Bananas   Grapes   Kiwis
2        3         nan      1
1        3         7        nan
nan      nan       2        3

I would like to add a column like this

Apples   Bananas   Grapes   Kiwis   Fruit Total
2        3         nan      1        6
1        3         7        nan      11
nan      nan       2        3        5

I guess you could use df['Apples'] + df['Bananas'] and so on, but my actual dataframe is much larger than this. I was hoping a formula like df['Fruit Total']=df[-4:-1].sum could do the trick in one line of code. That didn't work, however. Is there any way to do it without explicitly summing up all columns? How pandas sum multiple columns ?

Your answer

1 Answer

More Big Data Hadoop discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Big Data Hadoop Blogs

Guides, tips and career advice on Big Data Hadoop from JanBask experts.