Reputation: 1694
The is a dataframe(df1) like the following:
eye_l eye_r nose mouse ear eyebrow_l eyebrow_r ...
34_35_a -1_-1_-1 45_66_b 45_64_a 78_87_a -1_-1_-1 -1_-1_-1
35_38_a -1_-1_-1 75_76_b -1_-1_-1 38_79_a -1_-1_-1 -1_-1_-1
64_43_a -1_-1_-1 85_66_b 65_45_a 87_45_a -1_-1_-1 -1_-1_-1
....................................................................
I want to just delete the columns that all the value of a column is -1_-1_-1 (such as eye_r, eyebrow_1, eyebrow_r), and please notice that some columns may have a number (not all) of value is -1_-1_-1 will be kept.
I know that there is code like:
df1. drop(['eye_r', 'eyebrow_l', 'eyebrow_r '], axis=1, inplace=True)
But all the value is -1_-1_-1 of columns is not just three, there may be 100, and also there is a number of dataframes like such kind of dataframe. I want to process this issue in common method. Thanks
Upvotes: 1
Views: 36
Reputation: 863501
Use boolean indexing
with DataFrame.all
and change condition to !=
or
DataFrame.any
with inverting mask by ~
:
df = df.loc[:, (df != '-1_-1_-1').any()]
Or:
df = df.loc[:, ~(df == '-1_-1_-1').all()]
print (df)
eye_l nose mouse ear
0 34_35_a 45_66_b 45_64_a 78_87_a
1 35_38_a 75_76_b -1_-1_-1 38_79_a
2 64_43_a 85_66_b 65_45_a 87_45_a
Detail:
print (df != '-1_-1_-1')
eye_l eye_r nose mouse ear eyebrow_l eyebrow_r
0 True False True True True False False
1 True False True False True False False
2 True False True True True False False
print ((df != '-1_-1_-1').any())
eye_l True
eye_r False
nose True
mouse True
ear True
eyebrow_l False
eyebrow_r False
dtype: bool
Upvotes: 3