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"
CodePudding user response:
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