如何过滤数据框并立即制作一个子集 pandas [英] How to filter dataframe and make a subset at once pandas

查看:38
本文介绍了如何过滤数据框并立即制作一个子集 pandas 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据某些列对数据框进行子选择,同时根据不同的列过滤数据框.在 SQL 中它看起来像这样:

I am trying to make a subselection of a dataframe based on some columns, while at the same time filtering the dataframe based on a different column. In SQL it looks like this:

SELECT col1, col2, col3,
FROM table
WHERE colume_4 = some_value

我知道如何分两步完成,但我更喜欢一次操作.有谁知道如何在python中做到这一点?

I know how to do it in two steps, but I prefer doing it in one operation. Does anyone know how to do this in python?

推荐答案

使用 DataFrame.loc 结合 布尔索引:

df.loc[df.colume_4 == some_value, ['col1','col2','col3']]

这篇关于如何过滤数据框并立即制作一个子集 pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆