Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (50.2k points)

If I have a dataframe 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?

1 Answer

0 votes
by (107k points)

You can perform this by first selecting it by iloc and then apply sum:

df['Fruit Total']= df.iloc[:, -4:-1].sum(axis=1)

print (df)

   Apples  Bananas  Grapes  Kiwis  Fruit Total

0     2.0      3.0     NaN    1.0          5.0

1     1.0      3.0     7.0    NaN         11.0

2     NaN      NaN     2.0    3.0          2.0

And getting the total of all columns, you can use:

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

You can refer the following links for more information regarding the same:

  1.  https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iloc.html
  2. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.sum.html
If you wish to Learn more about Pandas visit this Pandas Tutorial.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...