I have two pandas Series: ser
and ovr
.
ser
contains objects, and ovr
is a sparse Series
of objects and None
's. ser
and ovr
share the same index, and I'd like to overwrite every value of ser
with its corresponding value of ovr
, unless that corresponding value is None
.
What's an efficient way to accomplish that?
I recommend using NaN for missing data rather than None (Note: this technique also works with None).
In [1]: s1 = pd.Series([1, np.nan, 3, 4, 5, np.nan])
In [2]: s2 = pd.Series([7, 2, 3, np.nan, np.nan])
First see that s2 values which are not NaN (or None), these are those which you want to update s1 with:
In [3]: s2[s2.notnull()]
Out[3]:
0 7
1 2
2 3
dtype: float64
And then you can update the values of s1 with these:
In [4]: s1.update(s2[s2.notnull()])
In [5]: s1
Out[5]:
0 7
1 2
2 3
3 4
4 5
5 NaN
dtype: float64
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With