简体   繁体   English

找出最频率组合并添加标签

[英]Find out the most frequency combination and add labels

I have a table with my customer data like this: 我有一个表格,其中包含我的客户数据:

Customer    Price
AAA            100
AAA            100
AAA            200
BBB            100
BBB            220
BBB            200
BBB            200

What I want to do is to find out the customer with the condition number of price >= 200 is more than number of price < 200 and add labels for them. 我想要做的是找出number of price >= 200 is more than number of price < 200的条件number of price >= 200 is more than number of price < 200并为它们添加标签。 for example: 例如:

Customer    LABELS
AAA            FALSE
BBB            TRUE

any ideas for this issue? 对这个问题的任何想法?

df.Price.ge(200).groupby(df.Customer).mean().gt(.5)

Customer
AAA    False
BBB     True
Name: Price, dtype: bool

Or if you insist on your format 或者如果你坚持你的格式

df.Price.ge(200).groupby(df.Customer).mean().gt(.5).reset_index(name='Labels')

  Customer  Labels
0      AAA   False
1      BBB    True

Straightforward answer: 直截了当的答案:

df.groupby('Customer').apply(
    lambda g: (g['Price'] >= 200).sum() > (g['Price'] < 200).sum()
)

Summing a boolean vector will return the number of True values. 求和布尔向量将返回True值的数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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