Fill NaN based on previous value of row

Idea is reshape DataFrame for possible forward and back filling missing values with stack and modulo and integer division of 2 of array by length of columns:

c = df.columns 
a = np.arange(len(df.columns))
df.columns = [a // 2, a % 2]

#if possible some pairs missing remove .astype(int)
df1 = df.stack().ffill(axis=1).bfill(axis=1).unstack().astype(int)
df1.columns = c
print (df1)
   A  B  C  D  E  F
0  3  4  3  4  3  4
1  9  8  9  8  9  8
2  5  9  4  7  4  7
3  5  7  6  3  6  3
4  2  6  4  3  4  3

Detail:

print (df.stack())
     0    1   2
0 0  3  NaN NaN
  1  4  NaN NaN
1 0  9  NaN NaN
  1  8  NaN NaN
2 0  5  4.0 NaN
  1  9  7.0 NaN
3 0  5  6.0 NaN
  1  7  3.0 NaN
4 0  2  4.0 NaN
  1  6  3.0 NaN

IIUC, a quick solution without reshaping the data:

df.iloc[:,::2] = df.iloc[:,::2].ffill(1)
df.iloc[:,1::2] = df.iloc[:,1::2].ffill(1)
df

Output:

   A  B  C  D  E  F
0  3  4  3  4  3  4
1  9  8  9  8  9  8
2  5  9  4  7  4  7
3  5  7  6  3  6  3
4  2  6  4  3  4  3

Tags:

Python

Pandas