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).
JavaScript
x
10
10
1
0 1
2
1 1
3
2 1
4
Name: A, dtype: int64
5
6
2 2
7
3 2
8
4 2
9
Name: B, dtype: int64
10
Into
JavaScript
1
7
1
0 1
2
1 1
3
2 0
4
3 2
5
4 2
6
Name: C, dtype: int64
7
How would I go about doing this in pandas.
Advertisement
Answer
We can do concat
then check the dup index , drop reindex
back
JavaScript
1
11
11
1
s = pd.concat([a,b])
2
s = s[~s.index.duplicated(keep=False)].reindex(s.index.unique(),fill_value=0)
3
out
4
Out[189]:
5
0 1
6
1 1
7
2 0
8
3 2
9
4 2
10
dtype: int64
11