Python大 pandas :在数据框中选择所有零条目的列 [英] Python pandas: select columns with all zero entries in dataframe

查看:137
本文介绍了Python大 pandas :在数据框中选择所有零条目的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个数据框如何找出只有0的所有列值作为值?

Given a dataframe how to find out all the columns that only have 0 as the values?

推荐答案

将值与0进行比较,并使用 .all()

I'd simply compare the values to 0 and use .all():

>>> df = pd.DataFrame(np.random.randint(0, 2, (2, 8)))
>>> df
   0  1  2  3  4  5  6  7
0  0  0  0  1  0  0  1  0
1  1  1  0  0  0  1  1  1
>>> df == 0
       0      1     2      3     4      5      6      7
0   True   True  True  False  True   True  False   True
1  False  False  True   True  True  False  False  False
>>> (df == 0).all()
0    False
1    False
2     True
3    False
4     True
5    False
6    False
7    False
dtype: bool
>>> df.columns[(df == 0).all()]
Int64Index([u'2', u'4'], dtype=int64)
>>> df.ix[:, (df == 0).all()]
   2  4
0  0  0
1  0  0

这篇关于Python大 pandas :在数据框中选择所有零条目的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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