0

Imagine two series:

A   B
0   nan
1   nan
0   3
1   8

How do I copy to column B the entries which appear in column A, unless that index has something already present in column B?

The only way I can think of to do this is using apply, like as follows, which seemingly is frowned upon by everyone.

df['B'] = df.apply(lambda r: r['A'] if r['B'].isnull() else r['B'], axis=1)
ifly6
  • 5,003
  • 2
  • 24
  • 47
  • 2
    `df['B'] = df['B'].fillna(df['A'])` should do. – harvpan Mar 28 '19 at 19:07
  • I knew I was forgetting something in the library. I ended up going with `df['B'].fillna(df['A'], inplace=True)` . Thanks! – ifly6 Mar 28 '19 at 19:09
  • I personally am not a big fan of [`inplace=True`](https://stackoverflow.com/questions/22532302/pandas-peculiar-performance-drop-for-inplace-rename-after-dropna/22533110#22533110). But that;s whole other discussion. Glad, I could help. :) – harvpan Mar 28 '19 at 19:14

0 Answers0