I have a Pandas DataFrame called df
(378000, 82) and I would like to replace the entire row with NaN
based on a specific condition. The condition is for any value in the column df.halon_gas
that is >20, I want to replace that entire row with NaN
. This is the way I want to filter my data so I don't lose the index values.
Thanks!
If you're fine with the rows being gone then I suggest you do this:
df.reset_index(level=0, inplace=True)
df = df[df.halon_gas <= 20]
df.set_index("index", inplace=True)
Whats happening here is the following:
First of all get all indexes of values, that are below 20
idx = df[df.halon_gas >= 20].index
Then set the values for all columns and all columns which are below 200 to None
df.set_value(idx, df.columns , None)
This should write None/Nan in the rows with the value below 20
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.