-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
eyrei123
wants to merge
5
commits into
master
Choose a base branch
from
how-to-drop-null-values-in-pandas
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import pandas as pd | ||
|
||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went for this:
but yours is simpler! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
,,,,, |
12 changes: 12 additions & 0 deletions
12
how-to-drop-null-values-in-pandas/sales_data_with_missing_values.csv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)