[英]Find out the most frequency combination and add labels
我有一个表格,其中包含我的客户数据:
Customer Price
AAA 100
AAA 100
AAA 200
BBB 100
BBB 220
BBB 200
BBB 200
我想要做的是找出number of price >= 200 is more than number of price < 200
的条件number of price >= 200 is more than number of price < 200
并为它们添加标签。 例如:
Customer LABELS
AAA FALSE
BBB TRUE
对这个问题的任何想法?
df.Price.ge(200).groupby(df.Customer).mean().gt(.5)
Customer
AAA False
BBB True
Name: Price, dtype: bool
或者如果你坚持你的格式
df.Price.ge(200).groupby(df.Customer).mean().gt(.5).reset_index(name='Labels')
Customer Labels
0 AAA False
1 BBB True
直截了当的答案:
df.groupby('Customer').apply(
lambda g: (g['Price'] >= 200).sum() > (g['Price'] < 200).sum()
)
求和布尔向量将返回True
值的数量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.