Yam*_*ing 2 python group-by frequency pandas pandas-groupby
我有一个表格,其中包含我的客户数据:
Customer Price
AAA 100
AAA 100
AAA 200
BBB 100
BBB 220
BBB 200
BBB 200
Run Code Online (Sandbox Code Playgroud)
我想要做的是找出具有条件的客户number of price >= 200 is more than number of price < 200
并为他们添加标签.例如:
Customer LABELS
AAA FALSE
BBB TRUE
Run Code Online (Sandbox Code Playgroud)
对这个问题的任何想法?
df.Price.ge(200).groupby(df.Customer).mean().gt(.5)
Customer
AAA False
BBB True
Name: Price, dtype: bool
Run Code Online (Sandbox Code Playgroud)
或者如果你坚持你的格式
df.Price.ge(200).groupby(df.Customer).mean().gt(.5).reset_index(name='Labels')
Customer Labels
0 AAA False
1 BBB True
Run Code Online (Sandbox Code Playgroud)