【发布时间】:2022-01-13 00:21:34
【问题描述】:
我在 Python Pandas 中有如下表,数据类型为 float64:
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 中做到这一点?
【问题讨论】:
-
使用
np.isnan(x.col1)或pd.isnull(x.col1)。 stackoverflow.com/questions/44367557/…
标签: python pandas dataframe lambda apply