Python Pandas-如何通过一个值过滤多列 [英] Python Pandas - How to filter multiple columns by one value

查看:282
本文介绍了Python Pandas-如何通过一个值过滤多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做熊猫分析.
我的表有7M行* 30列.单元格值的范围从-1到3随机.现在,我想根据列的值过滤出行.

我了解如何根据多个条件进行选择,写下条件并按&"组合"|".
但是我有30列要过滤,并且要按相同的值过滤.例如,需要选择最后12列的值等于-1

I am doing analysis by pandas.
My table has 7M rows* 30 columns. Cell values are ranged from -1 to 3 randomly. Now I want to filter out rows based on columns' value.

I understand how to select based on multiple conditions, write down conditions and combine by "&" "|".
But I have 30 columns to filter and filter by the same value. For instance, last 12 columns' value equals -1 need to be selected

df.iloc[:,-12:]==-1

上面的代码给了我一个布尔值.我需要实际的数据框.
此处的逻辑为或",表示如果任何列的值为-1,则需要选择该行. 另外,很高兴知道我是否需要和",所有列的值都为-1?
非常感谢

The code above gives me a boolean. I need actual data frame.
The logic here is "or", means if any column has value -1, that row needs to be selected. Also, it is good to know what if I need "and", all columns have value -1?
Many thanks

推荐答案

对于OR案例,请使用

For the OR case, use DF.any (returns True if any element is True along a particular axis):

df[(df.iloc[:,-12:] == -1).any(axis=1)]

对于AND情况,请使用 (如果所有元素沿特定轴为True,则返回True):

For the AND case, use DF.all (returns True if all elements are True along a particular axis):

df[(df.iloc[:,-12:] == -1).all(axis=1)]

这篇关于Python Pandas-如何通过一个值过滤多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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