Reputation: 3392
Given pandas data frame df
, I need to select those columns that only have positive values.
df =
Age Fare Dev
21 10 -1
35 9 0
28 12 1
My first idea was to use df.describe()
and then select only those columns that have minimum value greater or equal to 0.
But I got stuck with the implementation. Apparently row.columns
does not work, because Series does not have columns
property.
properties = df.describe()
positive_cols = []
for index,row in properties.iterrows():
for col in row.columns:
print(col)
Upvotes: 2
Views: 3136
Reputation: 862611
Use ge
(>=
) for compare DataFrame
. Then get boolean mask with all True
s by all
and last use loc
, because filtering columns:
df = df.loc[:, df.ge(0).all()]
print (df)
Age Fare
0 21 10
1 35 9
2 28 12
Details:
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
Upvotes: 5