pandas sum 两列数据合并保留Nan
From the documentation pandas.DataFrame.sum
By default, the sum of an empty or all-NA Series is 0.
>>> pd.Series([]).sum() # min_count=0 is the default 0.0
This can be controlled with the min_count parameter. For example, if you’d like the sum of an empty series to be NaN, pass min_count=1.
Change your code to
data.loc[:,’Sum’] = data.loc[:,[‘Surf1′,’Surf2’]].sum(axis=1, min_count=1)
原文
https://stackoverflow.com/questions/61636049/pandas-sum-of-two-columns-dealing-with-nan-values-correctly