How to fill the missing value with fixed number of consecutive period of a Data frame in python?

ipython

#1

I have created a Data Frame in Ipython notebook in which there are some missing value and I have used ffill method to fill the missing the value.

I want that ffill method only fill the first two NAN of each column and reaming NaN should remain as missing value.


#2

@sid100158,

Here, limit option of FILLNA will help: Look at the below commands:

#First Created a data frame
import pandas as pd
data=pd.DataFrame([[3,4,2],[1,2],[],[5,3]])
data

Above, you can see that data frame has missing values.

#Used ffill method to fill missing values
data.fillna(method='ffill')

fillna with ffill method has fill all missing values with previous non missing value

#Used option limit to restrict the value to fill
data.fillna(method='ffill',limit=2)

After using limit=2 option, it has only filled second third observation of third column not the last one.

Hope this helps!

Regards,
Mark