带条件和列的Python过滤数据框

标签 python pandas filtering

我很好奇是否可以按列和条件过滤 pandas DataFrame。或者我必须分两步完成。

所以我的基本想法是:

 df['cluster' & (df['Type'] == 't')]

或者通过两个步骤实现这一点是否没有开销,例如:

tmp = df[df['Type'] == 't']
tmp = tmp[df['Type']]

最佳答案

你可以这样做:

df.loc[df['Type'] == 't', 'cluster']

关于带条件和列的Python过滤数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49719017/

相关文章:

python - 如何通过引用传递变量?

python - 在子进程命令中使用变量 (Python)

python Pandas : How do I apply a function requiring an extended class (datetime)?

.net - Log4Net StringMatchFilter 不过滤任何内容

sql - 根据与小数点右侧匹配的值过滤数据的好方法是什么?

html - 我需要一个数据库来处理我的网站内容吗?

Python "if modified since"检测?

python - 如何制作具有两个单独循环的嵌套 for 循环?

pandas - 如何在 vscode 中打印出 Pandas 数据帧

python - 带次要 y 轴的 Pandas 条形图 : hide grid line below plot