for the following data frame:
index Sent col_1 col_2 col_3
1 AB NaN DD CC
1 0 1 0
2 SA FA FB NaN
2 1 1 NaN
3 FF Sha NaN PA
3 1 0 1
I need to replace NAN value in col_1, col_2, col_3 with "F" when NAN is not repeated in two Consecutive rows. The output is like this:
index Sent col_1 col_2 col_3
1 AB F DD CC
1 0 1 0
2 SA FA FB NaN
2 1 1 NaN
3 FF Sha F PA
3 1 0 1
This is my code:
for col in ['col_1', 'col_2', 'col_3']:
data = np.reshape(df[col].values, (-1, 2))
need_fill = np.logical_and(data[:, 0] == '', data[:, 1] != '')
data[np.where(need_fill),1] = 'F'
But it replace the 0 under NAN value to F. How I can fix the code to replace NAN to F.