python - 基于联合条件的 Pandas 切片行

标签 python pandas numpy

考虑下面的数据框-df

    one  two  three  four  five  six  seven  eight
0   0.1  1.1    2.2   3.3   3.6  4.1    0.0    0.0
1   0.1  2.1    2.3   3.2   3.7  4.3    0.0    0.0
2   1.6  0.0    0.0   0.0   0.0  0.0    0.0    0.0
3   0.1  1.2    2.5   3.7   4.4  0.0    0.0    0.0
4   1.7  2.1    0.0   0.0   0.0  0.0    0.0    0.0
5   2.1  3.2    0.0   0.0   0.0  0.0    0.0    0.0
6   2.1  2.3    3.2   4.3   0.0  0.0    0.0    0.0
7   2.2  0.0    0.0   0.0   0.0  0.0    0.0    0.0
8   0.1  1.8    0.0   0.0   0.0  0.0    0.0    0.0
9   1.6  0.0    0.0   0.0   0.0  0.0    0.0    0.0

我想选择所有 any 列值为“3.2”的行,但同时所选行的值不应为“0.1”或“1.2”

我可以通过以下查询获得第一部分

df[df.values == 3.2]

但不能将其与查询的第二部分(联合 != 条件)结合起来

我也得到以下错误

DeprecationWarning: elementwise != comparison failed; this will raise an error in the future.

尝试以下操作时在较大的数据集上(但不是在较小的副本上)

df[df.values != [0.1,1.2]]

//编辑:

@pensen,这是输出,第 1、15、27、35 行的值为“0.1”,但根据条件它们应该被过滤。

contains = df.eq(3.2).any(axis=1)
not_contains = ~df.isin([0.1,1.2]).any(axis=1)
print(df[contains & not_contains])
      0    1    2    3    4    5    6    7
1    0.1  2.1  3.2  0.0  0.0  0.0  0.0  0.0
15   0.1  1.1  2.2  3.2  3.3  3.6  3.7  0.0
27   0.1  2.1  2.3  3.2  3.6  3.7  4.3  0.0
31   3.2  0.0  0.0  0.0  0.0  0.0  0.0  0.0
35   0.1  1.7  2.1  3.2  3.6  3.7  4.3  0.0

这里是从 0:36 行复制上面输出的原始数据集

      0    1    2    3    4    5    6    7
 0   4.1  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 1   0.1  2.1  3.2  0.0  0.0  0.0  0.0  0.0
 2   0.1  2.4  2.5  0.0  0.0  0.0  0.0  0.0
 3   2.4  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 4   4.4  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 5   1.1  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 6   0.1  2.1  4.1  0.0  0.0  0.0  0.0  0.0
 7   4.4  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 8   1.7  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 9   2.2  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 10  1.1  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 11  1.1  4.1  0.0  0.0  0.0  0.0  0.0  0.0
 12  0.1  2.2  3.3  3.6  0.0  0.0  0.0  0.0
 13  0.1  1.8  3.3  0.0  0.0  0.0  0.0  0.0
 14  0.1  1.2  1.3  2.5  3.7  4.2  0.0  0.0
 15  0.1  1.1  2.2  3.2  3.3  3.6  3.7  0.0
 16  1.3  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 17  1.3  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 18  1.3  2.5  0.0  0.0  0.0  0.0  0.0  0.0
 19  0.1  1.2  2.5  3.7  4.4  0.0  0.0  0.0
 20  1.2  4.4  0.0  0.0  0.0  0.0  0.0  0.0
 21  4.3  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 22  1.1  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 23  0.1  2.2  2.4  2.5  3.7  0.0  0.0  0.0
 24  0.1  2.4  4.3  0.0  0.0  0.0  0.0  0.0
 25  1.7  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 26  0.1  1.1  4.1  0.0  0.0  0.0  0.0  0.0
 27  0.1  2.1  2.3  3.2  3.6  3.7  4.3  0.0
 28  1.4  2.2  3.6  4.1  0.0  0.0  0.0  0.0
 29  1.8  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 30  1.2  4.4  0.0  0.0  0.0  0.0  0.0  0.0
 31  3.2  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 32  3.6  4.1  0.0  0.0  0.0  0.0  0.0  0.0
 33  2.1  2.4  0.0  0.0  0.0  0.0  0.0  0.0
 34  0.1  1.8  0.0  0.0  0.0  0.0  0.0  0.0
 35  0.1  1.7  2.1  3.2  3.6  3.7  4.3  0.0

这里是 link到实际数据集

最佳答案

简而言之,您可以执行以下操作:

df.eq(3.2).any(axis=1) & ~df.isin([0.1, 1.2]).any(axis=1)

或者更明确地说:

contains = df.eq(3.2).any(axis=1)
not_contains = ~df.isin([0.1,1.2]).any(axis=1)

print(df[contains & not_contains])
   one    two    three    four    five    six    seven    eight
5  2.1    3.2    0.0      0.0     0.0     0.0    0.0      0.0
6  2.1    2.3    3.2      4.3     0.0     0.0    0.0      0.0

关于python - 基于联合条件的 Pandas 切片行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42542790/

相关文章:

python - 使用 groupby 对 Pandas DataFrame 进行计算,然后将其传回到 DataFrame 中?

python - 在 numpy 中查找最大值的索引排除零值

python - 高效计算多个张量内积

python - Numpy 广播添加

python - 如何将 URL 拆分为三个不同的变量

python - 如何将 [[A, B], [A, C], [A, D]] 列表转换为 Python 中的 {A : [B, C, D]} 字典?

python - QSignalTransition 子类从未收到自定义 PyQt 信号

python - 为什么在我的代码中 np.array**3 会导致与 (np.array/1)**3 不同的解决方案?

python - 为什么我只能从 statsmodels OLS 拟合中获得一个参数

python - 替换 pandas dataframe 中的值时出现 str 错误