Mamed
Mamed

Reputation: 772

Fill NaN based on previous value of row

I have a data frame (sample, not real):

df =

    A   B   C    D   E     F       

0   3   4   NaN  NaN NaN   NaN  
1   9   8   NaN  NaN NaN   NaN      
2   5   9   4    7   NaN   NaN  
3   5   7   6    3   NaN   NaN  
4   2   6   4    3   NaN   NaN  

Now I want to fill NaN values with previous couple(!!!) values of row (fill Nan with left existing couple of numbers and apply to the whole row) and apply this to the whole dataset.

Desired output is:

df =

   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  

Upvotes: 26

Views: 1328

Answers (2)

Quang Hoang
Quang Hoang

Reputation: 150745

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

Upvotes: 19

jezrael
jezrael

Reputation: 862641

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

Upvotes: 14

Related Questions