Search code examples
pythonpandasdataframemissing-dataimputation

How to insert missing values in dataframe (pandas) for specific rows?


I have a dataframe with 5 columns and 2500 rows, there are some missing values. And I need to add mean values instead of missing values for the first 1000 rows, use ffill method for the next 1000 and bfill for the last 500. Should I loop over the rows to do this? I only found how to use ffill and bfill methods for the whole dataframe. Please help with the logic here.

I tried df.iloc(1:1000) to slice the first part, but then failed to insert mean values.


Solution

  • Here is how to correctly use iloc:

    df.iloc[:1000] = df.iloc[:1000].fillna(df.mean())
    
    df.iloc[1000:2000] = df.iloc[1000:2000].fillna(method='ffill')
    
    df.iloc[2000:] = df.iloc[2000:].fillna(method='bfill')