2017-03-07 175 views
1

我有一些數據,並希望獲得具有小樣本大小的列標題(例如< 90%的總行數)。我如何得到它們的列表,可能是以列表或數據框的形式返回?Pandas:列滿足某些條件的列返回列標題

在下面的例子中,我想獲得FieldC作爲輸出。

使用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... 
+0

看起來像你想行標題,不列標題? 'train_df [train_df.count()<2000 * 0.9] .index' – DyZ

+0

@DYZ感謝您的評論。我也嘗試過,但我得到了錯誤'索引錯誤:不可調整的布爾系列鍵提供' –

+0

對不起。必須是'train_df.count()[train_df.count()<2000 * 0.9] .index'。或者,更有效的是,'count = train_df.count();計數[計數<2000 * 0.9] .index'。 – DyZ

回答

0

我認爲你可以這樣做:

columnsToBeReturn=[] 
max=df.shape[0] #getting the shape of the entire dataframe so the biggest number of rows 
for col in df.columns: 
    if len(df[col])<max*0.9: 
     columsToBeReturn.append(col) 
return columnsToBeReturn