Skip to content

how-to-drop-null-values-in-pandas #678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions how-to-drop-null-values-in-pandas/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
The materials contained in this download are designed to complement the RealPython tutorial [How to Drop Null Values in pandas](https://realpython.com/how-to-drop-null-values-in-pandas/).

You should create a new folder named pandas_nulls on your computer and place each file inside it. You may also consider creating a [Python virtual environment](https://realpython.com/python-virtual-environments-a-primer/) within this folder.

Your download bundle contains the following four files. The first three files contain the code from different tutorial sections, while the fourth contains the solutions to the exercise.

`drop_null_rows.py`
`drop_null_columns.py`
`drop_a_subset.py`
`exercise_solutions.py`

There are also two data files containing the data used throughout the tutorial:

`sales_data_with_missing_values.csv`
`grades.csv`

16 changes: 16 additions & 0 deletions how-to-drop-null-values-in-pandas/drop_a_subset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pandas as pd

sales_data = pd.read_csv(
"sales_data_with_missing_values.csv",
parse_dates=["order_date"],
date_format="%d/%m/%Y",
).convert_dtypes(dtype_backend="pyarrow")


sales_data.dropna(axis=0, subset=(["discount", "sale_price"]))

sales_data.dropna(how="all")

sales_data.dropna(thresh=5)

sales_data.dropna(thresh=5, ignore_index=True)
9 changes: 9 additions & 0 deletions how-to-drop-null-values-in-pandas/drop_null_columns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pandas as pd

sales_data = pd.read_csv(
"sales_data_with_missing_values.csv",
parse_dates=["order_date"],
date_format="%d/%m/%Y",
).convert_dtypes(dtype_backend="pyarrow")

sales_data.dropna(axis="columns")
17 changes: 17 additions & 0 deletions how-to-drop-null-values-in-pandas/drop_null_rows.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pandas as pd

sales_data = pd.read_csv(
"sales_data_with_missing_values.csv",
parse_dates=["order_date"],
date_format="%d/%m/%Y",
).convert_dtypes(dtype_backend="pyarrow")

sales_data

sales_data.isna().sum()

sales_data.dropna()

clean_sales_data = sales_data.dropna()

clean_sales_data = sales_data.dropna(inplace=True)
25 changes: 25 additions & 0 deletions how-to-drop-null-values-in-pandas/exercise solutions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pandas as pd
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps best to put an underscore in the script name to match the others (and for best practice)


grades = pd.read_csv(
"grades.csv",
).convert_dtypes(dtype_backend="pyarrow")

# 1. Permanently drop the last row of the dataframe.

grades.dropna(how="all", inplace=True)

# 2. Display the rows for the exams that all students have completed.

grades.dropna()

# 3. Display any columns with no missing data.

grades.dropna(axis=1)

# 4. Display the exams students have sat five or more times.

grades.dropna(axis=0, thresh=6) # Remember there are seven columns.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went for this:

grades.dropna(thresh=5, subset=grades.columns[1:])

but yours is simpler!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mind is simpler, remember. :-)


# 5. Who else would be in the exam hall when both `S2` and `S4` were there?

grades.dropna(subset=["S2", "S4"]).dropna(axis=1, ignore_index=True)
8 changes: 8 additions & 0 deletions how-to-drop-null-values-in-pandas/grades.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Subject,S1,S2,S3,S4,S5,S6
math,18,,15,20,17,18
science,26,35,19,,33,
art,15,,9,17,18,14
music,14,20,12,20,13,18
history,18,19,,17,,18
sport,20,17,20,17,18
,,,,,
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
order_number,order_date,customer_name,product_purchased,discount,sale_price
,09/02/2025,Skipton Fealty,Chili Extra Virgin Olive Oil,TRUE,135.00
70041,,Carmine Priestnall,,,150.00
70042,09/02/2025,,Rosemary Olive Oil Candle,FALSE,78.00
70043,10/02/2025,Lanni D'Ambrogi,,TRUE,19.50
70044,10/02/2025,Tann Angear,Vanilla and Olive Oil Candle,,13.98
70045,10/02/2025,Skipton Fealty,Basil Extra Virgin Olive Oil,TRUE,
70046,11/02/2025,Far Pow,Chili Extra Virgin Olive Oil,FALSE,150.00
70047,11/02/2025,Hill Group,Chili Extra Virgin Olive Oil,TRUE,135.00
70048,11/02/2025,Devlin Nock,Lavender and Olive Oil Lotion,FALSE,39.96
,,,,,
70049,12/02/2025,Swift Inc,Garlic Extra Virgin Olive Oil,TRUE,936.00