How to ffill missing value in Pandas

In this article, we are going to discuss how to ffill missing value in Pandas.We will learn pandas ffill by example code. The pandas dataframe fillna() method makes users replace nan or missing value or null with their own values.

1.How to ffill missing value in Pandas


The pandas ffill() function allows us to fill the missing value in dataframe.The ffill stand for forward fill ,replace the null values with value from previous row else column if axis set to axis = ‘columns’.

In this python program code example we will discuss how to forward fill missing value in all columns of pandas dataframe by passing fillna(method=ffill)

import pandas as pd
import numpy as np
  
Student_dict = {
    'Name': ['Jack', 'Rack', np.nan],
    'Marks':[100.5,np.nan, np.nan],
    'Subject': [np.nan, 'Math', 'Music']
}
  
 
 
dfobj = pd.DataFrame(Student_dict)

dfobj = dfobj.fillna(method="ffill")

print(dfobj)

Output

   Name  Marks Subject
0  Jack  100.5     NaN
1  Rack  100.5    Math
2  Rack  100.5   Music

2. Pandas ffill one column


Sometimes instead of forward fill whole pandas dataframe,we need to forward fill single column of pandas dataframe.In this code example we will understand how to forward fill single column of pandas dataframe.

import pandas as pd
import numpy as np
  
Student_dict = {
    'Name': ['Jack', 'Rack', np.nan],
    'Marks':[100.5,np.nan, np.nan],
    'Subject': [np.nan, 'Math', 'Music']
}
  
 
 
dfobj = pd.DataFrame(Student_dict)





dfobj["Subject"] = dfobj["Subject"].fillna(method="ffill")

print(dfobj)


Output

  Name  Marks Subject
0  Jack  100.5     NaN
1  Rack    NaN    Math
2   NaN    NaN   Music

3. Pandas ffill mutiple columns


In this code example we will understand how to fill multiple columns of pandas dataframe by using the ffill() function.We specify the name of columns we want forward fill and return a copy of dataframe after forward fill the values.

import pandas as pd
import numpy as np
  
Student_dict = {
    'Name': ['Jack', 'Rack', np.nan],
    'Marks':[100.5,np.nan, np.nan],
    'Subject': [np.nan, 'Math', 'Music']
}
  
 
 
dfobj = pd.DataFrame(Student_dict)




dfobj[['Name', 'Marks']] = dfobj[['Name', 'Marks']].fillna(method="ffill")  
print (dfobj)

Output

  Name  Marks Subject
0  Jack  100.5     NaN
1  Rack  100.5    Math
2  Rack  100.5   Musics

4. Pandas Groupby ffill missing values


In this pandas group by ffill example.we will learn how to groupby ffill missing/nan values in Pandas dataframe.if we do not want to fill other column with ‘nan’ then we can specify the column name using this code

dfobj['Marks'] = dfobj.groupby('Subject')['Marks'].ffill()
print(dfobj)

import pandas as pd
import numpy as np
  
Student_dict = {
    'Name': ['Jack','Rack','Max','Tony', np.nan],
    'Marks':[100,np.nan,80,90,100],
    'Subject': ['Math', 'Math', 'Music','Math',np.nan]
}
  


dfobj = pd.DataFrame(Student_dict)
dfobj.groupby('Subject').ffill()
print(dfobj)


Output

   Name  Marks Subject
0  Jack  100.0    Math
1  Rack  100.0    Math
2   Max   80.0   Music
3  Tony   90.0    Math
4   NaN  100.0     NaN

5. Pandas ffill zeros with previous non zero value


In this python code example we will learn how to replace zeros with previous non zero value by using the replace() method of pandas dataframe in which we have specify first argument zero value that we want to replace with ffill previous non zero value.

import pandas as pd
import numpy as np
  
Student_dict = {
    'Name': ['Jack','Rack','Max','Tony'],
    'Marks':[90,0,0,100],
    'Subject': ['Math', 'Math', 'Music','Math']
}

 
dfobj = pd.DataFrame(Student_dict)

dfobj['Marks'] = dfobj['Marks'].replace(to_replace=0, method='ffill')
print(dfobj)

Output

  Name  Marks Subject
0  Jack     90    Math
1  Rack     90    Math
2   Max     90   Music
3  Tony    100    Math

6. Pandas ffill with limit


The Pandas dataframe ffil() method limit parameter specify that maximum number of continuous nan values forward fill in Pandas dataframe.

import pandas as pd
import numpy as np
  
Student_dict = {
    'Name': ['Jack','Rack','Max',np.nan],
    'Marks':[90,90,np.nan,100],
    'Subject': ['Math', 'Math','Music',np.nan]
}

 
dfobj = pd.DataFrame(Student_dict)

print(dfobj.ffill(axis=0,limit=1))

Output

  Name  Marks Subject
0  Jack   90.0    Math
1  Rack   90.0    Math
2   Max   90.0   Music
3   Max  100.0   Music

Summary

In this post we have learned how to ffill missing value in Pandas by using dataframe function ffill(),fillna() and replace() and groupby() with program example for each.We can use any of them as per our need.