gpt4 book ai didi

python - 根据某些条件过滤数据

转载 作者:行者123 更新时间:2023-12-02 02:11:15 24 4
gpt4 key购买 nike

我有一个数据框

username   score
1 0.0008
1 0.1
1 0.000009
2 0.2
2 0.0098
2 0.7
3 0.99
3 0.019
3 0.0001

我需要使用该条件进行过滤

d = {1: 0.05, 2: 0.01, 3: 0.02}

所以我需要为每个客户提取所有小于字典中值的内容。

期望输出:

username   score
1 0.0008
1 0.000009
2 0.0098
3 0.019
3 0.0001

如何快速过滤?我知道可以使用循环来完成此操作,但我有一个包含 100 多个用户名的庞大数据集。

最佳答案

您可以map username用你的字典d然后使用 bool 索引,仅选择那些具有 score 的索引l ESS t汉映射结果:

df[df.score.lt(df.username.map(d))]

获取

   username     score
0 1 0.000800
2 1 0.000009
4 2 0.009800
7 3 0.019000
8 3 0.000100

关于python - 根据某些条件过滤数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67726112/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com