Amazing technological breakthrough possible @S-Logix pro@slogix.in

Office Address

  • #5, First Floor, 4th Street Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 Landmark : Samiyar Madam
  • pro@slogix.in
  • +91- 81240 01111

Social List

How to replace NaN values by using forward and backward methods in python?

Description

To replace the Nan(missing values) by using forward and backward methods using python.

Process

  Import pandas library.

  Read the sample data set.

  Find the missing values in the data set.

  Replace the missing values by using pad() and bfill().

Sample Code

#import libraries

import pandas as pd

#Read the sample data

data=pd.read_csv(‘/home/soft27/soft27

/Sathish/Pythonfiles/Houseprice.csv’)

#create data frame

df=pd.DataFrame(data)

print(“Actual data frame is:\n”,df)

#Find the missing values

print(“Find missing values”)

print(df.isnull())

#Replace the missing values(Nan) using forward

print(“Replace the Nan values”)

print(df.fillna(method=’pad’))

#Replace the missing values(Nan) using backward

print(df.fillna(method=’bfill’))

Screenshots
replace NaN values by using forward and backward methods in python
Read the sample data
Find the missing values
Replace the Nan values