How to replace Null values using Pandas

2 min readPandasDataFrameData ManipulationData
7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

The Pandas library will give you the tools to replace the Null values the same way as replacing NaN values.

Fillna

The .fillna() method will replace all Null values in contained in a pandas.Series ou pandas.DataFrame.

The method takes multiple arguments such as

Value

The value you want to replace the Null with.

Method

The method of fill. Can be forward fill as "ffill", backward fill as "bfill".

Axis

The axis you want to perform the operation on, it works only when given a DataFrame.

An example on a Serie

# My column is the column in which 
# we want to replace our Null 
df["my_column"] = df["my_column"].fillna(0)
The fillna method used to replace NaN values into 0

An example on a DataFrame

# Here we apply the method on a dataframe itself
df = df.fillna(0) # Here axis 1 means columns
The fillna method used to replace NaN values into 0

Here you are, you know how to replace your Null by a specific value !

If you want to learn more about Backfill or Forward Fill, you can check the links below

How to backward fill in Python
How to replace NaN values by the next available value. The simplest method using the fillna Pandas method.
How to forward fill in Python
How to replace NaN values by the latest available value. The simplest method using Pandas fillna method.
7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Free Newsletter

Master Data Science in Days, Not Months 🚀

Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚

Weekly simple and practical lessons
Access to ready to use code examples
Skip the math, focus on results
Learn while drinking your coffee

By subscribing, you agree to receive our newsletter. You can unsubscribe at any time.