I have a two columns in df, sometimes it has NaN in either one column, sometimes in both columns. I want to fill NaN with same value if any one of the columns values present.
For ex, Input:
col1 col2
0 3.375000 4.075000
1 2.450000 1.567100
2 NaN NaN
3 3.248083 NaN
4 NaN 2.335725
5 2.150000 3.218750
Output:
col1 col2
0 3.375000 4.075000
1 2.450000 1.567100
2 NaN NaN
3 3.248083 3.248083
4 2.335725 2.335725
5 2.150000 3.218750
For this I tried,
print df.T.fillna(method='bfill').fillna(method='ffill').T
The above give me a required result, But I think I'm adding more complexity to my code. Is there any other better approach for this?