Skip to content Skip to sidebar Skip to footer

Python(pandas) Fills Blanks Cells

I am using Python(Pandas) to manipulate high frequency data. Basically, I need to fill the blank cells. If the this row is blank, then this row will be filled in with the previous

Solution 1:

Use fillna() property. You can specify the method as forward fill as follows

import pandas as pd
data = pd.read_csv('sample.csv')
data = data.fillna(method='ffill') # This one forward fills all the columns.
# You can also apply to specific columns as below
# data[['bid','ask']] = data[['bid','ask']].fillna(method='ffill')
print data
    Time  bid      ask    
015:00  NaN      NaN
115:00  NaN      NaN
215:0276      NaN
315:027677415:037677515:037877615:047877715:057880815:057880915:057880

Solution 2:

There is the lesser known ffill method:

In [102]:
df.ffill()

Out[102]:
    Time  bid  ask
015:00NaNNaN115:00NaNNaN215:0276NaN315:027677415:037677515:037877615:047877715:057880815:057880915:057880

Post a Comment for "Python(pandas) Fills Blanks Cells"