简体   繁体   中英

How to group and calculate in pandas

I have df like below

customer class  score
A          a      10
A          b      20
B          a      40
B          b      50

I would like to group, transform and calculate like this.

customer  score(b-a)
A           10
B           10

I couldnt figure out how to calculate..

df.groupby(df.customer)

If someone has experienced such aggregation,please let me know.

Thanks

You can use @HenryYik's comment, or you can use pivot :

(df.pivot(index='customer', columns='class', values='score')
   .assign(score=lambda x: x['b']-x['a'])
)

Output:

class      a   b  score
customer               
A         10  20     10
B         40  50     10

Alternative solution, group by over customer and apply a custom function

def get_score(temp):
    map_score = dict(zip(temp['class'], temp['score'])) # mapping of class and score for each customer
    return map_score['b'] - map_score['a']
df.groupby("customer").apply(get_score)

This will result in expected answer.

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