0

If we have 2 series:

a = pd.Series([1, 2, 3, np.nan, 5, np.nan, np.nan])
b = pd.Series([np.nan, np.nan, np.nan, 4, np.nan, 6, 7])

How can I get c, such that

a                 b             c
========          ========      =========
0    1.0          0    NaN      0    1.0     
1    2.0          1    NaN      1    2.0
2    3.0          2    NaN      2    3.0
3    NaN          3    4.0      3    4.0
4    5.0          4    NaN      4    5.0
5    NaN          5    6.0      5    6.0
6    NaN          6    7.0      6    7.0

where c is the overlap of a & b, if the value is not a NaN (it will always be the case)

A H
  • 2,164
  • 1
  • 21
  • 36
  • 2
    `a.combine_first(b)` – cs95 Dec 28 '17 at 16:35
  • Hmm, the `add(fill_value=0)` isn't in the dupe. I'll add that. – cs95 Dec 28 '17 at 16:37
  • Or even `a.where(pd.notnull, b)` if we're just throwing options out there :) – Jon Clements Dec 28 '17 at 16:37
  • @DSM; actually, the dupe does not specify that the NaNs are in mutually exclusive positions (like this question seems to claim). So, the `add(fill_value)` method will not work there. Do you still think they're duplicates? – cs95 Dec 28 '17 at 16:45
  • @JonClements I came up with `a.mask(a.isnull(), b)`. I can tell you're getting good at this. – cs95 Dec 28 '17 at 16:45
  • @cᴏʟᴅsᴘᴇᴇᴅ, your solution is marginally faster: combine_first = 991 µs ± 28 µs per loop mask = 923 µs ± 13.4 µs per loop – A H Dec 28 '17 at 17:00
  • @AH Cheers, glad it worked. Go to https://stackoverflow.com/questions/38152389/coalesce-values-from-2-columns-into-a-single-column-in-a-pandas-dataframe and upvote (or downvote) the answers if you could. – cs95 Dec 28 '17 at 17:01
  • @cᴏʟᴅsᴘᴇᴇᴅ done, thanks. – A H Dec 28 '17 at 17:30
  • Thanks for not downvoting ;-) – cs95 Dec 28 '17 at 17:32

0 Answers0