gpt4 book ai didi

python - 通过设置值选择 Pandas Dataframe

转载 作者:太空宇宙 更新时间:2023-11-04 05:44:40 26 4
gpt4 key购买 nike

让下面的 Pandas Dataframe df,如何找到值 6 和 10 的行?

    0   1   2   3   4   5   6
0 11 1 3 4 6 8 10
1 11 1 3 4 6 8 11
2 11 1 3 4 6 8 0
3 11 1 3 4 6 9 10
4 11 1 3 4 6 9 11
5 11 1 3 4 6 9 0
6 11 1 3 4 6 10 10
7 11 1 3 4 6 10 11
8 11 1 3 4 6 10 0
9 11 1 3 4 7 8 10

我可以通过基于集合的解决方案获得这些行:

>>> df.iloc[[i for i, s in enumerate(df.itertuples()) if {6, 10} <= set(s)]]

0 1 2 3 4 5 6
0 11 1 3 4 6 8 10
3 11 1 3 4 6 9 10
6 11 1 3 4 6 10 10
7 11 1 3 4 6 10 11
8 11 1 3 4 6 10 0

我的问题是:在 Pandas 中是否有更好的方法在存在这些给定值的行中获得 True ?比如:

df.where({6, 10} <= df)

数据示例:

pandas.DataFrame.from_dict({0: {0: 11, 1: 11, 2: 11, 3: 11, 4: 11, 5: 11, 6: 11, 7: 11, 8: 11, 9: 11},
1: {0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1},
2: {0: 3, 1: 3, 2: 3, 3: 3, 4: 3, 5: 3, 6: 3, 7: 3, 8: 3, 9: 3},
3: {0: 4, 1: 4, 2: 4, 3: 4, 4: 4, 5: 4, 6: 4, 7: 4, 8: 4, 9: 4},
4: {0: 6, 1: 6, 2: 6, 3: 6, 4: 6, 5: 6, 6: 6, 7: 6, 8: 6, 9: 7},
5: {0: 8, 1: 8, 2: 8, 3: 9, 4: 9, 5: 9, 6: 10, 7: 10, 8: 10, 9: 8},
6: {0: 10, 1: 11, 2: 0, 3: 10, 4: 11, 5: 0, 6: 10, 7: 11, 8: 0, 9: 10}})

编辑

这个数据框只是我真实数据的一小部分。0 到 11 之间的整数在每一行中可以出现 0 到 2 次。例如,在这些行中,值 4、8 和 11 各出现两次。

        0   1   2   3   4   5   6
100 11 1 4 4 8 8 11
343 11 2 4 4 8 8 11
505 11 3 3 4 8 8 11
586 11 3 4 4 8 8 11
1558 1 1 4 4 8 8 11

最佳答案

您可以使用 isin 来测试成员资格,然后调用 dropna 并传递 thresh=2 以仅显示至少有 2 的行存在非 NaN 值:

In [20]:
df[df.isin([6,10])].dropna(thresh=2)

Out[20]:
0 1 2 3 4 5 6
0 NaN NaN NaN NaN 6 NaN 10
3 NaN NaN NaN NaN 6 NaN 10
6 NaN NaN NaN NaN 6 10 10
7 NaN NaN NaN NaN 6 10 NaN
8 NaN NaN NaN NaN 6 10 NaN

我认为实际上最好测试每个值并应用any:

In [41]:
df.apply(lambda x: (x == 6).any() & (x == 10).any(), axis=1)

Out[41]:
0 True
1 False
2 False
3 True
4 False
5 False
6 True
7 True
8 True
9 False
dtype: bool

对于 3 个值,您可以执行以下操作:

df.apply(lambda x: (x==5).any() & (x == 6).any() & (x == 10).any(), axis=1)

关于python - 通过设置值选择 Pandas Dataframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32739751/

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