我在帶有 float64 資料型別的 Python Pandas 中有如下表:
col1
--------
245.121
NaN
44.908
我嘗試使用以下代碼創建新列“col2”:
data["col2"] = data.apply(lambda x: 1 if x.col1== np.nan else 0, axis = 1)
不幸的是,當我使用上面的代碼時,我到處都是 0,為什么?我如何修改我的代碼以實作如下所示的功能:
col1 col2
--------
245.121 | 0
NaN | 1
44.908 | 0
我怎樣才能在 Python Pandas 中做到這一點?
uj5u.com熱心網友回復:
嘗試:
data["col2"] = data.apply(lambda x: 1 if x.col1 in [np.nan] else 0, axis = 1)
這應該作業,雖然你不會因為它的一個特點是np.nan != np.nan
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/376268.html
