5

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 Answer 1

7

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
Sign up to request clarification or add additional context in comments.

2 Comments

thank you. is there a way to drop the NaNs?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.