计算值高于阈值的列数

问题描述 投票:0回答:2

假设我有以下玩具模型,

df
:

product    customer1    customer2    customer3      
apple           40           110          120
banana         200           150          180
coconut         10             5           25
daq            120            10           30
eclair          45           190           35

我想在

df
中添加一列,用于统计购买了至少一百种所列商品的客户数量:

product    customer1    customer2    customer3   atleast100    
apple           40           110          120             2
banana         200           150          180             3
coconut         10             5           25             0
daq            120            10           30             1
eclair          45           190           35             1
python pandas dataframe count statistics
2个回答
5
投票

customer
列中,使用
ge().sum()
计算每行中大于或等于 100 的值的数量。

df['atleast100'] = df.filter(like='customer').ge(100).sum(axis=1)
print(df)

   product  customer1  customer2  customer3  atleast100
0    apple         40        110        120           2
1   banana        200        150        180           3
2  coconut         10          5         25           0
3      daq        120         10         30           1
4   eclair         45        190         35           1

3
投票

试试这个:

df['atleast100'] = df[df.columns[1:]].gt(100).sum(axis=1)
print(df)

   product  customer1  customer2  customer3  atleast100
0    apple         40        110        120           2
1   banana        200        150        180           3
2  coconut         10          5         25           0
3      daq        120         10         30           1
4   eclair         45        190         35           1
© www.soinside.com 2019 - 2024. All rights reserved.