两列相加时的NaN值 [英] NaN values when adding two columns
本文介绍了两列相加时的NaN值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个具有不同索引的数据框,我想对两个数据框的同一列求和. 我尝试了以下操作,但给出了NaN值
I have two dataframes with different indexing that I want to sum the same column from the two dataframes. I tried the following but gives NaN values
result['Anomaly'] = df['Anomaly'] + tmp['Anomaly']
df
date Anomaly
0 2018-12-06 0
1 2019-01-07 0
2 2019-02-06 1
3 2019-03-06 0
4 2019-04-06 0
tmp
date Anomaly
0 2018-12-06 0
1 2019-01-07 1
4 2019-04-06 0
result
date Anomaly
0 2018-12-06 0.0
1 2019-01-07 NaN
2 2019-02-06 1.0
3 2019-03-06 0.0
4 2019-04-06 0.0
我想要的实际上是:
result
date Anomaly
0 2018-12-06 0
1 2019-01-07 1
2 2019-02-06 1
3 2019-03-06 0
4 2019-04-06 0
推荐答案
您可以尝试
pd.concat([df, tmp]).groupby('date', as_index=False)["Anomaly"].sum()
date Anomaly
0 2018-12-06 0
1 2019-01-07 1
2 2019-02-06 1
3 2019-03-06 0
4 2019-04-06 0
这篇关于两列相加时的NaN值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文