从Pandas数据框中过滤仅零的列 [英] Filter columns of only zeros from a Pandas data frame
本文介绍了从Pandas数据框中过滤仅零的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Pandas数据框,我想在其中过滤掉仅包含零的所有列.例如,在下面的数据框中,我想删除第2列:
I have a Pandas Data Frame where I would like to filter out all columns which only contain zeros. For example, in the Data Frame below, I'd like to remove column 2:
0 1 2 3 4
0 0.381 0.794 0.000 0.964 0.304
1 0.538 0.029 0.000 0.327 0.928
2 0.041 0.312 0.000 0.208 0.284
3 0.406 0.786 0.000 0.334 0.118
4 0.511 0.166 0.000 0.181 0.980
我该怎么做?我一直在尝试这样的事情:
How can I do this? I've been trying something like this:
df.filter(lambda x: x == 0)
推荐答案
以下内容对我有用.它给出了一个系列,其中列名现在是索引,而索引的值为True/False取决于列中的所有项目是否为0.
The following works for me. It gives a series where column names are now the index, and the value for an index is True/False depending on whether all items in the column are 0.
import pandas, numpy as np
# Create DataFrame "df" like yours...
df.apply(lambda x: np.all(x==0))
这篇关于从Pandas数据框中过滤仅零的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文