Home > database >  Pandas ffill on section of DataFrame
Pandas ffill on section of DataFrame

Time:06-09

I am attempting to forward fill a filtered section of a DataFrame but it is not working the way I hoped.

I have df that look like this:

    Col Col2    
0   1   NaN 
1   NaN NaN 
2   3   string  
3   NaN string  

I want it to look like this:

    Col Col2    
0   1   NaN 
1   NaN NaN 
2   3   string  
3   3   string  

This my current code:

filter = (df["col2"] == "string")
df.loc[filter, "col"].fillna(method="ffill", inplace=True)

But my code does not change the df at all. Any feedback is greatly appreciated

CodePudding user response:

I am not sure I understand your question but if you want to fill the NAN values or any values you should use the Simple imputer

from sklearn.impute import SimpleImputer

Then you can define an imputer that fills these missing values/NAN with a specific strategy. For example if you want to fill these values with the mean of all the column you can write it as follows:

imputer=SimpleImputer(missing_values=np.nan, strategy= 'mean')

Or you can write it like this if you have the NaN as string

imputer=SimpleImputer(missing_values="NaN", strategy= 'mean')

and if you want to fill it with a specific values you can do this:

imputer=SimpleImputer(missing_values=np.nan, strategy= 'constant', fill_value = "YOUR VALUE")

Then you can use it like that

df[["Col"]]=imputer.fit_transform(df[["Col"]])

CodePudding user response:

We can use boolean indexing to filter the section of Col where Col2 = 'string' then forward fill and update the values only in that section

m = df['Col2'].eq('string')
df.loc[m, 'Col'] = df.loc[m, 'Col'].ffill()

   Col    Col2
0  1.0     NaN
1  NaN     NaN
2  3.0  string
3  3.0  string
  • Related