根据行条件过滤 pandas 列 [英] Filter pandas columns based on row condition
本文介绍了根据行条件过滤 pandas 列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下称为df的数据框.
i have the following dataframe called df.
x1 x2 x3 ....
row1 12 3.4 5 ...
row2 1 3 4 ...
row3 True False True ...
...
我想显示所有row3值都为True的列.
I want to display the columns where all the row3 values are True.
在此示例中将显示列 x1
和 x3
.
so like columns x1
and x3
will be displayed in this example.
当我想根据列信息过滤行时,我知道如何做,但是当我想根据行过滤时,我不确定如何做.
I know how to do it the other way when i want to filter rows based on column info but im not sure how to do it when i want to filter based on a row.
我尝试过
df.loc[:,df.iloc['row3']==True]
但是我得到一个错误.
我该怎么做?有什么想法吗?
How do i do this? any ideas?
推荐答案
iloc
要求您传递整数(列表).尝试 loc
:
iloc
asks you to pass (list of) integers. Try loc
:
df.loc[:,df.loc['row3']]
这篇关于根据行条件过滤 pandas 列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文