簡體   English   中英

將熊貓系列替換為長度不同但索引相同的系列

[英]Replace pandas Series with Series of different length but same indices

假設兩只熊貓系列A和B:

A:

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

具有更多基本熊貓運算符的替代解決方案。

a.loc[b.index.values]=b.values

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM