I have the following dataframe called df.
Sometimes it looks like this (where each value is unique)
key value1 value2 value3
0 key1 value value value
1 key2 value value value
Sometimes the entire column of value3
are is filled with dashes: '-'
.
key value1 value2 value3
0 key1 value value -
1 key2 value value -
I want to find a command which will drop the column value3 if all the items in the column dashes.
I tried using df['value3'].any()
and that that returns a '-'
.
Is the right way to do what I want this?
if df['value3'].any() == '-':
df = df.drop['value3']
Or is there a better way?
Use
>>> df.loc[:, ~df.eq('-').all()]
key value1 value2
0 key1 value value
1 key2 value value
Compare for not equal by DataFrame.ne
and then get all columns with match by DataFrame.any
in boolean indexing
:
print (df.loc[:, df.ne('-').any()])
key value1 value2
0 key1 value value
1 key2 value value
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.