简体   繁体   中英

Pandas: How to assign the value to the first row of a group after groupby?

I tried to assign the value to the first row of a group after groupby?

Input:

ID Num
Aa 100
Bb 200
Cc 300
Bb 400
Aa 500

Output:

ID Num Sum
Aa 100 600
Aa 500
Bb 200 600
Bb 400
Cc 300 300

Code:

base_df['Sum'] = base_df.groupby(['ID'])['Num'].transform('sum')

However, this code will assign the value 'Sum' to every row in the group:

My output:

ID Num Sum
Aa 100 600
Aa 500 600
Bb 200 600
Bb 400 600
Cc 300 300

You can assign using duplicated :

s = df.groupby(['ID'])['Num'].transform('sum')
df.loc[~df.duplicated("ID"), "Sum"] = s
print (df)

   ID  Num    Sum
0  Aa  100  600.0
1  Bb  200  600.0
2  Cc  300  300.0
3  Bb  400    NaN
4  Aa  500    NaN

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