4

trying to sum rows for specific columns in pandas.

have:

df =

name    age gender  sales   commissions
joe     25  m       100     10
jane    55  f       40      4

want:

df =
name    age gender  sales   commissions
joe     25  m       100     10
jane    55  f       40      4
            
Total               140     14

I've tried this option but it's aggregating everything:

df.loc['Total'] = df.sum()
nia4life
  • 333
  • 1
  • 5
  • 17

1 Answers1

5

You can sum the columns of interest only:

## recreate your data
df = pd.DataFrame({'name':['joe','jane'],'age':[25,55],'sales':[100,40],'commissions':[10,4]})

df.loc['Total'] = df[['sales','commissions']].sum()

Result:

>>> df
       name   age  sales  commissions
0       joe  25.0  100.0         10.0
1      jane  55.0   40.0          4.0
Total   NaN   NaN  140.0         14.0

If you don't want the NaN to appear, you can replace them with an empty string: df = df.fillna('')

Result:

>>> df
       name   age  sales  commissions
0       joe  25.0  100.0         10.0
1      jane  55.0   40.0          4.0
Total              140.0         14.0
Derek O
  • 16,770
  • 4
  • 24
  • 43
  • thank you. is there a way to drop the NaNs? – nia4life May 01 '21 at 23:36
  • 1
    Every entry of the DataFrame has to be filled, but you can replace `NaN` with an empty string so that when you print the DataFrame, it doesn't display. But you should know that those entries that appear empty, in fact, contain a string object. – Derek O May 02 '21 at 04:20