Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwriting (updating) a pandas Series with values from another Series?

Tags:

python

pandas

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?

like image 960
Dun Peal Avatar asked Sep 24 '13 22:09

Dun Peal


1 Answers

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
like image 187
Andy Hayden Avatar answered Sep 20 '22 17:09

Andy Hayden