gpt4 book ai didi

python - 如何过滤 DataFrame 列表中的数字(n>3)?

转载 作者:太空宇宙 更新时间:2023-11-03 21:19:41 24 4
gpt4 key购买 nike

movie_id    user_id      rating
0 1 [5, 2, 1, 6] [4, 4, 5, 4]
1 2 [5, 1] [3, 3]
2 3 [1] [4]
3 4 [1] [3]
4 5 [1] [3]
5 6 [1] [5]
6 7 [6, 1] [2, 4]
7 8 [1, 6] [1, 4]
8 9 [1, 6] [5, 4]

我正在尝试获取每行“评级”中大于 3 的数字计数。例如,[4, 4, 5, 5] => 4/[3, 3] => 0。

这是我到目前为止所做的:

appr = df.copy()

appr['approval'] = appr['rating'].map(Counter)
appr

它输出:

    movie_id    user_id   rating        approval
0 1 [5, 2, 1, 6][4, 4, 5, 4] {4: 3, 5: 1}
1 2 [5, 1] [3, 3] {3: 2}
2 3 [1] [4] {4: 1}
3 4 [1] [3] {3: 1}
4 5 [1] [3] {3: 1}
5 6 [1] [5] {5: 1}
6 7 [6, 1] [2, 4] {2: 1, 4: 1}
7 8 [1, 6] [1, 4] {1: 1, 4: 1}
8 9 [1, 6] [5, 4] {5: 1, 4: 1}

我的目标是过滤掉每行“评级”中不大于 3 的数字,并对它们的出现次数求和:

    movie_id    user_id   rating        approval       appr_sum
0 1 [5, 2, 1, 6][4, 4, 5, 4] {4: 3, 5: 1} 4
1 2 [5, 1] [3, 3] {3: 2} 0
2 3 [1] [4] {4: 1} 1
3 4 [1] [3] {3: 1} 0
4 5 [1] [3] {3: 1} 0
5 6 [1] [5] {5: 1} 1
6 7 [6, 1] [2, 4] {2: 1, 4: 1} 1
7 8 [1, 6] [1, 4] {1: 1, 4: 1} 1
8 9 [1, 6] [5, 4] {5: 1, 4: 1} 2

我已经尝试过:

s = appr['rating'].map

t = [x for x in s if x > 3]
t

但是有一个 TypeError: 'method' object is not iterable,如果这部分代码正确,它不会对它们的出现进行求和。

最佳答案

使用带有过滤和求和的嵌套列表理解:

appr['appr_sum'] = [sum(v for k, v in x.items() if k > 3) for x in appr['approval']]
print (appr)
movie_id user_id rating approval appr_sum
0 1 [5, 2, 1, 6] [4, 4, 5, 4] {4: 3, 5: 1} 4
1 2 [5, 1] [3, 3] {3: 2} 0
2 3 [1] [4] {4: 1} 1
3 4 [1] [3] {3: 1} 0
4 5 [1] [3] {3: 1} 0
5 6 [1] [5] {5: 1} 1
6 7 [6, 1] [2, 4] {2: 1, 4: 1} 1
7 8 [1, 6] [1, 4] {1: 1, 4: 1} 1
8 9 [1, 6] [5, 4] {5: 1, 4: 1} 2

关于python - 如何过滤 DataFrame 列表中的数字(n>3)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54403747/

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