Python pandas:选择数据框中所有零条目的列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16486762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:47:56 来源:igfitidea点击:
Python pandas: select columns with all zero entries in dataframe
提问by user511792
Given a dataframe how to find out all the columns that only have 0 as the values?
给定一个数据框,如何找出所有只有 0 作为值的列?
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
Expected output
预期输出
2 4
0 0 0
1 0 0
采纳答案by DSM
I'd simply compare the values to 0 and use .all():
我只是将这些值与 0 进行比较并使用.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.loc[:, (df == 0).all()]
2 4
0 0 0
1 0 0

