如何在不复制密钥的情况下连接两个熊猫系列



系列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

我想你想要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

最新更新