Sum Rows at Bottom of Pandas Dataframe

Viewed 1646

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()
1 Answers

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
Related