簡體   English   中英

找出最頻率組合並添加標簽

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM