![](/img/trans.png)
[英]Pandas Dataframe - replace NaN with 0 if column value condition
[英]Replace a row in Pandas DataFrame with 'NaN' based on condition
我有一個名為df
(378000, 82) 的 Pandas DataFrame ,我想根據特定條件用NaN
替換整行。 條件是df.halon_gas
列中大於 20 的任何值,我想用NaN
替換整行。 這是我想要過濾數據的方式,這樣我就不會丟失索引值。
謝謝!
如果您對消失的行感到滿意,那么我建議您這樣做:
df.reset_index(level=0, inplace=True)
df = df[df.halon_gas <= 20]
df.set_index("index", inplace=True)
這里發生的情況如下:
首先獲取值的所有索引,低於 20
idx = df[df.halon_gas >= 20].index
然后將所有列和所有低於 200 的列的值設置為 None
df.set_value(idx, df.columns , None)
這應該在值低於 20 的行中寫入 None/Nan
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.