假设有两只 Pandas 系列 A 和 B:
答:
1 4
2 4
3 4
4 1
5 3
B:
3 4
4 4
5 2
A 大于 B,并且 B 与 A 具有相同的索引,但值不同。我正在尝试用 B 的值替换 A 的值。
A.replace(to_replace=B)
看起来很明显,但不起作用。我在这里缺少什么?
最佳答案
我认为你可以使用combine_first
:
C = B.combine_first(A).astype(int)
print (C)
1 4
2 4
3 4
4 4
5 2
dtype: int32
关于python - 将 pandas Series 替换为不同长度但相同索引的 Series,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45630516/