Home > Mobile >  Combine two pandas Series with overlap into one column
Combine two pandas Series with overlap into one column

Time:09-01

Let us say I have two Pandas series A and B that I wish to combine into one series. I want to set the values of overlapping rows to 0 (or some other arbitrary value).

0    1
1    1
2    1
Name: A, dtype: int64

2    2
3    2
4    2
Name: B, dtype: int64

Into

0    1
1    1
2    0
3    2
4    2
Name: C, dtype: int64

How would I go about doing this in pandas.

CodePudding user response:

We can do concat then check the dup index , drop reindex back

s = pd.concat([a,b])
s = s[~s.index.duplicated(keep=False)].reindex(s.index.unique(),fill_value=0)
out
Out[189]: 
0    1
1    1
2    0
3    2
4    2
dtype: int64
  • Related