Suppose I have a dataframe that contains columns with lots and lots of nan values - in fact most values are none, except one (or a few that are identical), but are distributed along different lines. For example:
df = pd.DataFrame({'A':[np.nan, 2, np.nan], 'B':[3.5, np.nan, 3.5], 'C':[np.nan, np.nan, 0.1]})
So how can I achieve a dataframe that looks like this?
A B C
0 2 3.5 0.1
1 2 3.5 0.1
2 2 3.5 0.1
'bfill' would only work for column 'C', 'ffill' only for column 'B'...
So how can I replace all the nan values in the column with the notna value present anywhere and in any number of instances in that column?
CodePudding user response:
Forwardfill, backfill the dataframe.
df =df.ffill().bfill()