nimbus3000
nimbus3000

Reputation: 210

Copying values into Nan fields

I have a large data-frame with the format as below:

enter image description here

If any of the cell is "NaN", i want to copy from the cell immediately above it. So, my dataframe should look like:

enter image description here

In case the first row has "NaN", then I'll have to let it be.

Can someone please help me with this?

Upvotes: 0

Views: 109

Answers (1)

EdChum
EdChum

Reputation: 394129

This looks like pandas if so you need to call ffill

In [72]:
df = pd.DataFrame({'A':['A0','A1','A2',np.NaN,np.NaN, 'A3'], 'B':['B0','B1','B2',np.NaN,np.NaN, 'B3'], 'C':['C0','C1','C2',np.NaN,np.NaN, 'C3']})
df

Out[72]:
     A    B    C
0   A0   B0   C0
1   A1   B1   C1
2   A2   B2   C2
3  NaN  NaN  NaN
4  NaN  NaN  NaN
5   A3   B3   C3

In [73]:    
df.ffill()

Out[73]:
    A   B   C
0  A0  B0  C0
1  A1  B1  C1
2  A2  B2  C2
3  A2  B2  C2
4  A2  B2  C2
5  A3  B3  C3

Upvotes: 1

Related Questions