pandas :返回满足特定条件的列的列标题 [英] Pandas: Return column headers for columns satisfying certain criteria

查看:62
本文介绍了 pandas :返回满足特定条件的列的列标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据,并希望获得样本量较小(例如,行总数小于90%)的列的列标题.如何获取它们的列表,也许以列表或数据框的形式返回?

I have some data and want to get the column headers for columns with a small sample size (eg < 90% total rows). How do I get a list of them, perhaps returned as either a list or a dataframe?

在下面的示例中,我想获取FieldC作为输出.

In the example below, I will like to get FieldC as output.

使用train_df.head():

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2000 entries, 0 to 1999
Data columns (total 100 columns):
Id               2000 non-null int64
FieldA           2000 non-null int64
FieldB           2000 non-null object
FieldC           1675 non-null float64
FieldD           2000 non-null int64
FieldE           2000 non-null object
...more fields...

使用train_df.count()<2000*0.9:

Id               False
FieldA           False
FieldB           False
FieldC           True
FieldD           False
FieldE           False
...more fields...

推荐答案

>>> _=pandas.DataFrame({'horse':[3,None],'cow':[1,2],'sheep':[None,None]})
>>> _
   cow  horse sheep
0    1    3.0  None
1    2    NaN  None
>>> criterion2=_.columns[_.count()>2]
>>> criterion1=_.columns[_.count()>1]
>>> criterion0=_.columns[_.count()>0]
>>> criterion2
Index([], dtype='object')
>>> criterion1
Index(['cow'], dtype='object')
>>> criterion0
Index(['cow', 'horse'], dtype='object')
>>> _[criterion2]
Empty DataFrame
Columns: []
Index: [0, 1]
>>> _[criterion1]
   cow
0    1
1    2
>>> _[criterion0]
   cow  horse
0    1    3.0
1    2    NaN
>>> pandas.__version__
'0.22.0'

此pandas.'object'的索引也可以转换为文本字符串序列.

This pandas.Index of 'object' can also be converted to a sequence of text strings.

这篇关于 pandas :返回满足特定条件的列的列标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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