如何选择只有正值的列?

Sca*_*Boy 2 python pandas

鉴于熊猫数据框df,我需要选择那些只有正值的列。

df =
  Age    Fare     Dev
  21     10        -1
  35     9        0
  28     12       1
Run Code Online (Sandbox Code Playgroud)

我的第一个想法是使用df.describe()然后只选择那些最小值大于或等于 0 的列。但我在实现上陷入了困境。显然row.columns不起作用,因为 Series 没有columns属性。

properties = df.describe()
positive_cols = []
for index,row in properties.iterrows():
    for col in row.columns:
        print(col)
Run Code Online (Sandbox Code Playgroud)

jez*_*ael 5

使用ge( >=) 进行比较DataFrame。然后使用所有Trues byall和 last use获取布尔掩码loc,因为过滤列:

df = df.loc[:, df.ge(0).all()]
print (df)
   Age  Fare
0   21    10
1   35     9
2   28    12
Run Code Online (Sandbox Code Playgroud)

详情

print (df.ge(0))
    Age  Fare    Dev
0  True  True  False
1  True  True  False
2  True  True   True

print (df.ge(0).all())
Age      True
Fare     True
Dev     False
dtype: bool
Run Code Online (Sandbox Code Playgroud)