系列a:
one 1
two 1
three 1
系列b:
two 2
three 2
four 2
如何合并它们以获得以下结果:
one 1
two 1
three 1
four 2
任何重复的密钥,请使用该密钥的系列a中的值。
我试过了:
pd.merge(a.reset_index(),b.reset_index(),how =' outer')
但得到:
one 1 nan
two 1 2
three 1 2
four nan 2
答案 0 :(得分:2)
我想你想要combine_first
方法:
In [86]: s1 = pd.Series(1, index=[1,2,3])
In [87]: s2 = pd.Series(2, index=[2,3,4])
In [88]: s1.combine_first(s2)
Out[88]:
1 1
2 1
3 1
4 2
dtype: float64