简体   繁体   中英

Calculate the sum of values based on another column value in pandas dataframe?

Sample dataframe

country   result    name  
  UK        3       created
  US        5       created
  UK        7       created
  India     4       completed
  India     6       created
  US        1       completed

Expected output

country   result    name  
  UK        3       created
  US        5       created
  UK        7       created
  India     4       completed
  India     6       created
  US        1       completed
  UK        10      pending       
  US        6       pending
  India     10      pending

I need to add the created and completed result on each country and it should have a name called "pending"

You can try groupby.sum then pd.concat

out = df.groupby('country', as_index=False)['result'].sum()
out['name'] = 'pending'
out = pd.concat([df, out], ignore_index=True)
print(out)

  country  result       name
0      UK       3    created
1      US       5    created
2      UK       7    created
3   India       4  completed
4   India       6    created
5      US       1  completed
6   India      10    pending
7      UK      10    pending
8      US       6    pending

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM