计算具有高于阈值的值的列数

发布于 2025-01-30 16:40:45 字数 789 浏览 1 评论 0原文

假设我有以下玩具模型,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

Assuming I have the following toy model, 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

I would like to add a column to df that counts the number of customers that bought at least a hundred of the items listed:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霞映澄塘 2025-02-06 16:40:45

客户列中,使用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

Among the customer columns, count the number of values greater or equal to 100 in each row using ge().sum().

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
叹沉浮 2025-02-06 16:40:45

尝试以下操作:

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

Try this:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文