python - Pandas :如果特定列满足特定条件,则选择行

标签 python pandas dataframe slice

假设我有这个数据框df:

    A   B   C
0   1   1   2
1   2   2   2
2   1   3   1
3   4   5   2

假设您要选择 C 列为 >1 的所有行。如果我这样做:

newdf=df['C']>1

我只在生成的 df 中获得 TrueFalse。相反,在给出的示例中,我想要这样的结果:

    A   B   C
0   1   1   2
1   2   2   2
3   4   5   2

你会怎么做?您是否建议使用 iloc

最佳答案

使用boolean indexing :

newdf=df[df['C']>1]

关于python - Pandas :如果特定列满足特定条件,则选择行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41147389/

相关文章:

python: 无法打开文件 get-pip.py 错误 2] 没有这样的文件或目录

python - 你将如何优化这个简短但非常慢的 Python 循环?

python - 排序数据框并按单词搜索

python - 在 Pandas DataFrame 中添加新列时结果不一致。它是系列还是值?

python - 使用python实现异构csv的数据结构

python - 使用 modelformsets ('ManagementForm data is missing or has been tampered with' 时管理表单错误)

python - (M,N) 和 (N,) 数组之间的距离计算

python - 将 DataFrame Pandas 中第二行的列分类到第一行?

python - 在Python中使用 bool 逻辑隔离pandas列

python - 使用 Pandas 石斑鱼时如何取系列的最大值?