Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Combine two pandas Series with overlap into one column

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading