根据行条件过滤熊猫列

时间:2021-02-24 19:01:13

标签: python pandas filter

我有以下名为 df 的数据框。

        x1    x2   x3 ....
row1    12   3.4    5  ...
row2     1     3    4 ...
row3  True False True ...
...

我想显示所有 row3 值为 True 的列。

因此,在此示例中将显示列 x1x3

当我想根据列信息过滤行时,我知道如何以另一种方式执行此操作,但当我想根据行进行过滤时,我不知道该怎么做。

我试过了

df.loc[:,df.iloc['row3']==True]

但我收到一个错误。

我该怎么做?有什么想法吗?

1 个答案:

答案 0 :(得分:2)

iloc 要求您传递(列表)整数。试试loc

df.loc[:,df.loc['row3']]