简体   繁体   中英

How to aggregate and groupby in pandas

I have following df,I'd like to group by customer and then, count and sum

at the same time,I wish add conditional grouping.

are there any way to achieve this?

customer  product score
A          a       1
A          b       2
A          c       3
B          a       4
B          a       5
B          b       6

My desired result is following

customer count sum count(product =a)  sum(product=a)
A          3    6     1                   1
B          3   15     2                   9

My work is like this..

grouped=df.groupby('customer')

grouped.agg({"product":"count","score":"sum"})

Thanks

Let us try crosstab

s = pd.crosstab(df['customer'],df['product'], df['score'],margins=True, aggfunc=['sum','count']).drop('All')
Out[76]: 
          sum               count              
product     a    b    c All     a    b    c All
customer                                       
A         1.0  2.0  3.0   6   1.0  1.0  1.0   3
B         9.0  6.0  NaN  15   2.0  1.0  NaN   3
import pandas as pd

df = pd.DataFrame({'customer': ['A', 'A', 'A', 'B', 'B', 'B'], 'product': ['a', 'b', 'c', 'a', 'a', 'b'], 'score':[1, 2, 3, 4, 5, 6]})
df = df[df['product']=='a']
grouped=df.groupby('customer')
grouped = grouped.agg({"product":"count","score":"sum"}).reset_index()
print(grouped)

Output:

  customer  product  score
0        A        1      1
1        B        2      9

Then merge this dataframe with the unfiltered grouped dataframe

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