[英]Select columns in pandas dataframe by value in rows
I have a pandas.DataFrame
with too much columns. 我有一个包含太多列的
pandas.DataFrame
。 I want to select all columns with values in rows equals to 0
and 1
. 我想选择行数等于
0
和1
所有列。 Type of all columns is int64
and I can't select they by object
or other type. 所有列的类型都是
int64
,我不能按object
或其他类型选择它们。 How can I do this? 我怎样才能做到这一点?
IIUC then you can use isin
and filter the columns: IIUC那么你可以使用
isin
和过滤列:
In [169]:
df = pd.DataFrame({'a':[0,1,1,0], 'b':list('abcd'), 'c':[1,2,3,4]})
df
Out[169]:
a b c
0 0 a 1
1 1 b 2
2 1 c 3
3 0 d 4
In [174]:
df[df.columns[df.isin([0,1]).all()]]
Out[174]:
a
0 0
1 1
2 1
3 0
The output from the inner condition: 内部条件的输出:
In [175]:
df.isin([0,1]).all()
Out[175]:
a True
b False
c False
dtype: bool
We can use the boolean mask to filter the columns: 我们可以使用布尔掩码来过滤列:
In [176]:
df.columns[df.isin([0,1]).all()]
Out[176]:
Index(['a'], dtype='object')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.