How to replace NaN values in Python
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.
The Pandas library will give you the tools to replace the nan values.
Fillna
The .fillna() method will replace all NaN 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 Na 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
An example on a DataFrame
Here you are, you know how to replace your NaN by a specific value !
If you want to learn more about Backfill or Forward Fill, you can check the links below
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.
Related Articles
Continue your learning journey with these related topics
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. 📚