【发布时间】:2021-08-06 14:51:28
【问题描述】:
如何比较两个 Series 并保留 NaN 值?例如:
s1 = pd.Series([np.nan, 1, 3])
s2 = pd.Series([0, 2, 3])
s1.eq(s2).astype(int)
输出:
0 0
1 0
2 1
dtype: int64
想要的结果:
0 NaN
1 0.0
2 1.0
dtype: float64
【问题讨论】:
-
如果保留 NaN,则 False 和 True 将变为 float 0/1。除非你使用这个:pandas.pydata.org/pandas-docs/stable/user_guide/boolean.html
-
@MarkWang 好点。我会尝试重写我的问题。
标签: pandas numpy comparison nan missing-data