Learn Pandas Together (Part 3): Deal With (Tricky) Null Values

Clara Y
2 min readMar 20, 2024

Not a Medium member? You can read this tutorial here.

Dealing with null values in datasets is crucial, but we often overlook them, especially when we are busy learning other Pandas features.

But to know how to deal with nulls, with even just a little bit of exercise, can give you much confidence.

I used commented code snippets in this tutorial to showcase the two most important operations related to null values.

The original code was generated from Poe. I modified it to make sure it is clean and runnable.

import pandas as pd
import numpy as np

# Create a sample wine data with null values in it. Note that we need numpy.
data = {
'Country': ['France', 'Italy', 'Spain', 'France', 'Italy', 'Spain'],
'Region': ['Bordeaux', 'Tuscany', 'Rioja', 'Burgundy', 'Piedmont','Rioja'],
'Points': [87, 95, 84, 97, 85, 89],
'Price': [15, np.NaN, 13, 64, np.NaN, 13],
'Variety': ['Merlot', 'Sangiovese', 'Tempranillo', 'Pinot Noir', 'Barbera',
'Tempranillo'],
'Reviewer': ['John', 'Emily', 'Michael', 'Sophia', 'William','Michael'],
'Vintage': [2010, 2012, 2015, 2013, 2011,2015],
'Description': ['Fra - Bor - Mer', 'Ita - Tus - San', 'Spa - Rio - Tem',
'Fra - Bur - Pin', 'Ita - Pie - Bar'…

--

--

Clara Y

I love to write about Art/Writing and Tech innovations that enrich the world around us. ☕️ Tip me: buymeacoffee.com/cdpenny