I have a DataFrame
like this:
>>> df = pd.DataFrame({'a': list('ABCD'), 'b': ['E',np.nan,np.nan,'F']})
a b
0 A E
1 B NaN
2 C NaN
3 D F
I am trying to fill NaN
with values of the previous column in the next row and dropping this second row. In other words, I want to combine the two rows with NaNs to form a single row without NaNs like this:
a b
0 A E
1 B C
2 D F
I have tried various flavors of df.fillna(method="<bfill/ffill>")
but this didn't give me the expected output.
I haven't found any other question about this problem, Here's one. And actually that DataFrame
is made from list of DataFrame
by doing .concat()
, you may notice that from indexes also. I am telling this because it may be easy to do in single row rather then in multiple rows.
I have found some suggestions to use shift
, combine_first
but non of them worked for me. You may try these too.
I also have found this too. It is a whole article about filling nan
values but I haven't found problem/answer like mine.