python - 将 pandas Series 替换为不同长度但相同索引的 Series

标签 python pandas

假设有两只 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/

相关文章:

Python tkinter 8.5 导入消息框

python - 从对中创建 pandas 数据框的快速方法

python-3.x - 使用 np.where 或 loc 更新 pandas 数据框的多列

python - 从数据帧中提取子集

python - 为什么从一个模块中导入一个函数比整个模块本身需要更长的时间?

python - 如何从Excel获取字典的多个键和单个值

python - 使用 Pandas 加载 CSV 数据

python - 必须使用某种集合调用索引 : assign column name to dataframe

python - Pandas 蟒 : how to get values on data frame every 10 steps?

python - 列表中的负数错误。输出错误。