đŸ•·ïž Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 71 (from laksa194)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

â„č Skipped - page is already crawled

📄
INDEXABLE
✅
CRAWLED
19 hours ago
đŸ€–
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://realpython.com/how-to-drop-null-values-in-pandas/
Last Crawled2026-04-11 02:45:03 (19 hours ago)
First Indexed2025-08-26 16:35:29 (7 months ago)
HTTP Status Code200
Meta TitleHow to Drop Null Values in pandas With .dropna() – Real Python
Meta DescriptionLearn how to use .dropna() to drop null values from pandas DataFrames so you can clean missing data and keep your Python analysis accurate.
Meta Canonicalnull
Boilerpipe Text
by Ian Eyre Publication date Sep 10, 2025 Reading time estimate 18m basics data-science python Missing values can derail your analysis. In pandas, you can use the .dropna() method to remove rows or columns containing null values—in other words, missing data—so you can work with clean DataFrames. In this tutorial, you’ll learn how this method’s parameters let you control exactly which data gets removed. As you’ll see, these parameters give you fine-grained control over how much of your data to clean. Dealing with null values is essential for keeping datasets clean and avoiding the issues they can cause. Missing entries can lead to misinterpreted column data types, inaccurate conclusions, and errors in calculations. Simply put, nulls can cause havoc if they find their way into your calculations. By the end of this tutorial, you’ll understand that: You can use .dropna() to remove rows and columns from a pandas DataFrame. You can remove rows and columns based on the content of a subset of your DataFrame. You can remove rows and columns based on the volume of null values within your DataFrame. To get the most out of this tutorial, it’s recommended that you already have a basic understanding of how to create pandas DataFrames from files. You’ll use the Python REPL along with a file named sales_data_with_missing_values.csv , which contains several null values you’ll deal with during the exercises. Before you start, extract this file from the downloadable materials by clicking the link at the end of this section. The sales_data_with_missing_values.csv file is based on the publicly available and complete sales data file from Kaggle . Understanding the file’s content isn’t essential for this tutorial, but you can explore the Kaggle link above for more details if you’d like. You’ll also need to install both the pandas and PyArrow libraries to make sure all code examples work in your environment: Windows Linux + macOS It’s time to refine your pandas skills by learning how to handle missing data in a variety of ways. You’ll find all code examples and the sales_data_with_missing_values.csv file in the materials for this tutorial, which you can download by clicking the link below: Take the Quiz: Test your knowledge with our interactive “How to Drop Null Values in pandas” quiz. You’ll receive a score upon completion to help you track your learning progress: How to Drop Rows Containing Null Values With pandas .dropna() Before you start dropping rows, it’s helpful to know what options .dropna() gives you. This method supports six parameters that let you control exactly what’s removed: axis : Specifies whether to remove rows or columns containing null values. thresh and how : Define how many missing values to remove or retain. subset : Limits the removal of null values to specific parts of your DataFrame. inplace : Determines whether the operation modifies the original DataFrame or returns a new copy. ignore_index : Resets the DataFrame index after removing rows. Don’t worry if any of these parameters don’t make sense to you just yet—you’ll learn why each is used during this tutorial. You’ll also get the chance to practice your skills. Before using .dropna() to drop rows, you should first find out whether your data contains any null values : To make sure all columns appear on your screen, you configure pd.set_option("display.max_columns", None) . By passing None as the second parameter, you make sure all columns are displayed. You read the sales_data_with_missing_values.csv file into a DataFrame using the pandas read_csv() function, then view the data. The order dates are in the "%d/%m/%Y" format in the file, so to make sure the order_date data is read correctly, you use both the parse_dates and date_format parameters. The output reveals there are ten rows and six columns of data in your file. In a real data analysis , your DataFrame may be too large to allow you to see everything that’s missing. To solve this, you use the DataFrame’s .isna() and .sum() methods together: As you can see, each of your columns is missing data. When you use sales_data.isna() , you create a Boolean DataFrame the same size as sales_data , but with null values replaced with True and everything else replaced with False . The .sum() method counts each True and returns it to you as shown. To remove the rows with null values, you use .dropna() : As you can see, rows with null values are nowhere to be seen. You might think the rows containing the null values from sales_data have been deleted. After all, that’s what the output shows. However, this isn’t true. When you use sales_data.dropna() , the results are placed into a new DataFrame, leaving the original unchanged. If you want to retain this second DataFrame, then you need to assign the output from .dropna() to a new variable: Now the clean_sales_data DataFrame contains a second copy of the original sales_data DataFrame, but without those rows that contained one or more null values. Of course, this is wasteful of memory, so a better option would be to update the original DataFrame. To do this, you pass inplace=True to .dropna() : The original sales_data DataFrame will no longer contain rows with null values, and any future analysis will occur against this new version. You’ve now seen the basic use case for .dropna() , where you drop rows with at least one missing value. But the method also includes several other parameters worth exploring. How to Drop Columns Containing Null Values With pandas .dropna() So far, you’ve seen how to drop rows containing missing values, but it’s also possible to drop incomplete columns. You do this using the axis parameter. By default, axis is set to 0 , or "index" , which means it operates on rows of data. If you want to apply it across columns, then you set axis to 1 or "columns" . Suppose you want to remove incomplete columns containing null values from the original sales_data . Here’s how you do it: Because every column has at least one missing value, they’ve all been removed, leaving the DataFrame empty except for its index. So far, you’ve taken a fairly aggressive approach by removing entire rows and columns. Next, you’ll look at some less destructive alternatives. How to Work With a Part of Your Data Earlier, you used .dropna() to remove all null values from your DataFrame. While this is a common use case, you can also remove nulls from specific parts of the DataFrame or based on a certain quantity threshold. You’ll learn how in this section. Removing Data Based on Specific Rows or Columns You can restrict removal of data based on the rows or columns where the null values appear. This is where the subset parameter comes in handy. By passing a column label or a sequence of labels to subset , you tell .dropna() which columns to check for null values and remove their associated rows. Similarly, by passing subset an index position or a sequence of index positions and setting axis="columns" , you remove the column or columns that contain null values at the specified index positions. For example, suppose you want to remove rows containing null values in either the discount or sale_price columns . If there are null values in other columns besides discount or sale_price , then those rows will remain untouched. Here’s how you do it: This time, you’ve removed four rows. To clarify that you’re removing rows, you explicitly set axis to 0 . However, you can omit this argument since 0 is the default value for the axis parameter. To restrict your analysis to the two columns you want, you pass the Python list ["discount", "sale_price"] as the subset parameter. Removing Data Based on the Quantity of Missing Values It’s possible to instruct .dropna() to remove rows or columns based on a specified quantity of data. Suppose you’re concerned about the volume of data missing in your DataFrame. You’re worried about rows with more than one missing value. In other words, you want to delete those rows containing at least five pieces of data in their six columns. To do this, you can set this threshold using the thresh parameter: Rows with indices 1 and 9 have been removed since they contain multiple missing values. By setting thresh to 5 , you tell .dropna() to keep all rows that contain at least five values. Since there are six columns, all remaining rows have no more than one missing value. Removing Empty Rows or Columns Sometimes you might want to remove only rows or columns that are completely empty, rather than those with just some missing values. You can control this with the how parameter. For example, suppose you want to remove rows where all values are missing: As you can see, the blank row at index position 9 has been removed. By passing how="all" to .dropna() and using the default value axis=0 , you remove rows containing only null values. Alternatively, you could pass how="any" to remove rows or columns that have at least one null value. You rarely need to do this, however, because "any" is the default value. Adding a Finishing Touch: Reset the Index Finally, you may have noticed that each time you remove a row—both here and throughout this tutorial—the index doesn’t update. As a finishing touch, passing ignore_index=True will reindex your DataFrame sequentially: This time, the index starts at 0 and ends at 8 , and there are only nine rows remaining. To wrap up what you’ve covered so far, it’s time to test your new skills. Practice Your pandas .dropna() Skills To practice, begin by loading the contents of grades.csv into a DataFrame: The DataFrame shows results for six students in five subjects. In this case, <NA> indicates that an exam wasn’t taken. Now, see if you can use .dropna() to answer each of the following questions: Use .dropna() so that it permanently drops the row in the DataFrame containing only null values. Use this version from this point on. Display the rows for the exams that all students completed. Display any columns with no missing data. Display the exams taken by at least five students. Identify who else completed every exam that both S2 and S4 took. You’ll find the answers in the downloadable materials below: Conclusion During this tutorial, you learned to use the DataFrame’s .dropna() method to remove null values from your DataFrames. By using the various parameters, you’ve learned how: The axis parameter allows you to remove rows or columns containing null values. The thresh and how parameters allow you to define the quantity of what gets removed. The subset parameter lets you restrict the removal to part of your DataFrame. The inplace parameter allows you to update either the original DataFrame or a copy after deletions. The ignore_index parameter allows you to reset the DataFrame index after rows of data have been removed. Although you now have a solid understanding of .dropna() , there’s still lots to explore. Review what you’ve learned here to identify which techniques best fit your needs for handling missing values. You may also want to apply these skills to the .dropna() methods for pandas Series and Index objects. Doing so is a great way to continue your learning journey. Take the Quiz: Test your knowledge with our interactive “How to Drop Null Values in pandas” quiz. You’ll receive a score upon completion to help you track your learning progress: Frequently Asked Questions Now that you have some experience with dropping null values in pandas, you can use the questions and answers below to check your understanding and recap what you’ve learned. These FAQs are related to the most important concepts you’ve covered in this tutorial. Click the Show/Hide toggle beside each question to reveal the answer. You use the .dropna() method to remove rows or columns with null values from your DataFrame. You remove empty values by using the .dropna() method, which lets you drop rows or columns containing null values. You use the thresh parameter to specify the minimum number of non-null values a row or column must have to avoid being dropped. You set the axis parameter to 1 or "columns" in the .dropna() method to drop columns containing null values. When you set inplace=True , you update the original DataFrame directly, removing rows or columns with null values without creating a new DataFrame.
Markdown
[![Real Python](https://realpython.com/static/real-python-logo.893c30edea53.svg)](https://realpython.com/) - [Start Here](https://realpython.com/start-here/) - [Learn Python](https://realpython.com/how-to-drop-null-values-in-pandas/) [Python Tutorials → In-depth articles and video courses](https://realpython.com/search?kind=article&kind=course&order=newest) [Learning Paths → Guided study plans for accelerated learning](https://realpython.com/learning-paths/) [Quizzes & Exercises → Check your learning progress](https://realpython.com/quizzes/) [Browse Topics → Focus on a specific area or skill level](https://realpython.com/tutorials/all/) [Community Chat → Learn with other Pythonistas](https://realpython.com/community/) [Office Hours → Live Q\&A calls with Python experts](https://realpython.com/office-hours/) [Live Courses → Live, instructor-led Python courses](https://realpython.com/live/) [Podcast → Hear what’s new in the world of Python](https://realpython.com/podcasts/rpp/) [Books → Round out your knowledge and learn offline](https://realpython.com/products/books/) [Reference → Concise definitions for common Python terms](https://realpython.com/ref/) [Code Mentor →Beta Personalized code assistance & learning tools](https://realpython.com/mentor/) [Unlock All Content →](https://realpython.com/account/join/) - [More](https://realpython.com/how-to-drop-null-values-in-pandas/) [Learner Stories](https://realpython.com/learner-stories/) [Python Newsletter](https://realpython.com/newsletter/) [Python Job Board](https://www.pythonjobshq.com/) [Meet the Team](https://realpython.com/team/) [Become a Contributor](https://realpython.com/jobs/) - [Search](https://realpython.com/search "Search") - [Join](https://realpython.com/account/join/) - [Sign‑In](https://realpython.com/account/login/?next=%2Fhow-to-drop-null-values-in-pandas%2F) [Browse Topics](https://realpython.com/tutorials/all/) [Guided Learning Paths](https://realpython.com/learning-paths/) [Basics](https://realpython.com/search?level=basics) [Intermediate](https://realpython.com/search?level=intermediate) [Advanced](https://realpython.com/search?level=advanced) *** [ai](https://realpython.com/tutorials/ai/) [algorithms](https://realpython.com/tutorials/algorithms/) [api](https://realpython.com/tutorials/api/) [best-practices](https://realpython.com/tutorials/best-practices/) [career](https://realpython.com/tutorials/career/) [community](https://realpython.com/tutorials/community/) [databases](https://realpython.com/tutorials/databases/) [data-science](https://realpython.com/tutorials/data-science/) [data-structures](https://realpython.com/tutorials/data-structures/) [data-viz](https://realpython.com/tutorials/data-viz/) [devops](https://realpython.com/tutorials/devops/) [django](https://realpython.com/tutorials/django/) [docker](https://realpython.com/tutorials/docker/) [editors](https://realpython.com/tutorials/editors/) [flask](https://realpython.com/tutorials/flask/) [front-end](https://realpython.com/tutorials/front-end/) [gamedev](https://realpython.com/tutorials/gamedev/) [gui](https://realpython.com/tutorials/gui/) [machine-learning](https://realpython.com/tutorials/machine-learning/) [news](https://realpython.com/tutorials/news/) [numpy](https://realpython.com/tutorials/numpy/) [projects](https://realpython.com/tutorials/projects/) [python](https://realpython.com/tutorials/python/) [stdlib](https://realpython.com/tutorials/stdlib/) [testing](https://realpython.com/tutorials/testing/) [tools](https://realpython.com/tutorials/tools/) [web-dev](https://realpython.com/tutorials/web-dev/) [web-scraping](https://realpython.com/tutorials/web-scraping/) [Table of Contents](https://realpython.com/how-to-drop-null-values-in-pandas/#toc) - [How to Drop Rows Containing Null Values With pandas .dropna()](https://realpython.com/how-to-drop-null-values-in-pandas/#how-to-drop-rows-containing-null-values-with-pandas-dropna) - [How to Drop Columns Containing Null Values With pandas .dropna()](https://realpython.com/how-to-drop-null-values-in-pandas/#how-to-drop-columns-containing-null-values-with-pandas-dropna) - [How to Work With a Part of Your Data](https://realpython.com/how-to-drop-null-values-in-pandas/#how-to-work-with-a-part-of-your-data) - [Removing Data Based on Specific Rows or Columns](https://realpython.com/how-to-drop-null-values-in-pandas/#removing-data-based-on-specific-rows-or-columns) - [Removing Data Based on the Quantity of Missing Values](https://realpython.com/how-to-drop-null-values-in-pandas/#removing-data-based-on-the-quantity-of-missing-values) - [Removing Empty Rows or Columns](https://realpython.com/how-to-drop-null-values-in-pandas/#removing-empty-rows-or-columns) - [Adding a Finishing Touch: Reset the Index](https://realpython.com/how-to-drop-null-values-in-pandas/#adding-a-finishing-touch-reset-the-index) - [Practice Your pandas .dropna() Skills](https://realpython.com/how-to-drop-null-values-in-pandas/#practice-your-pandas-dropna-skills) - [Conclusion](https://realpython.com/how-to-drop-null-values-in-pandas/#conclusion) - [Frequently Asked Questions](https://realpython.com/how-to-drop-null-values-in-pandas/#frequently-asked-questions) Mark as Completed Share ![How to Drop Null Values in pandas](https://files.realpython.com/media/How-to-Drop-Null-Values-in-Pandas_Watermarked.f06e0d54bea4.jpg) # How to Drop Null Values in pandas With .dropna() by [Ian Eyre](https://realpython.com/how-to-drop-null-values-in-pandas/#author) Publication date Sep 10, 2025 Reading time estimate 18m [0 Comments](https://realpython.com/how-to-drop-null-values-in-pandas/#reader-comments) [basics](https://realpython.com/tutorials/basics/) [data-science](https://realpython.com/tutorials/data-science/) [python](https://realpython.com/tutorials/python/) Mark as Completed Share Table of Contents - [How to Drop Rows Containing Null Values With pandas .dropna()](https://realpython.com/how-to-drop-null-values-in-pandas/#how-to-drop-rows-containing-null-values-with-pandas-dropna) - [How to Drop Columns Containing Null Values With pandas .dropna()](https://realpython.com/how-to-drop-null-values-in-pandas/#how-to-drop-columns-containing-null-values-with-pandas-dropna) - [How to Work With a Part of Your Data](https://realpython.com/how-to-drop-null-values-in-pandas/#how-to-work-with-a-part-of-your-data) - [Removing Data Based on Specific Rows or Columns](https://realpython.com/how-to-drop-null-values-in-pandas/#removing-data-based-on-specific-rows-or-columns) - [Removing Data Based on the Quantity of Missing Values](https://realpython.com/how-to-drop-null-values-in-pandas/#removing-data-based-on-the-quantity-of-missing-values) - [Removing Empty Rows or Columns](https://realpython.com/how-to-drop-null-values-in-pandas/#removing-empty-rows-or-columns) - [Adding a Finishing Touch: Reset the Index](https://realpython.com/how-to-drop-null-values-in-pandas/#adding-a-finishing-touch-reset-the-index) - [Practice Your pandas .dropna() Skills](https://realpython.com/how-to-drop-null-values-in-pandas/#practice-your-pandas-dropna-skills) - [Conclusion](https://realpython.com/how-to-drop-null-values-in-pandas/#conclusion) - [Frequently Asked Questions](https://realpython.com/how-to-drop-null-values-in-pandas/#frequently-asked-questions) [Remove ads](https://realpython.com/account/join/) Missing values can derail your analysis. In pandas, you can use the `.dropna()` method to remove rows or columns containing null values—in other words, missing data—so you can work with clean DataFrames. In this tutorial, you’ll learn how this method’s parameters let you control exactly which data gets removed. As you’ll see, these parameters give you fine-grained control over how much of your data to clean. Dealing with null values is essential for keeping datasets clean and avoiding the issues they can cause. Missing entries can lead to misinterpreted column data types, inaccurate conclusions, and errors in calculations. Simply put, nulls can cause havoc if they find their way into your calculations. **By the end of this tutorial, you’ll understand that:** - You can use **`.dropna()`** to **remove rows and columns** from a pandas DataFrame. - You can remove rows and columns based on the **content of a subset** of your DataFrame. - You can remove rows and columns based on the **volume of null values** within your DataFrame. To get the most out of this tutorial, it’s recommended that you already have a basic understanding of how to create [pandas DataFrames](https://realpython.com/pandas-dataframe/) from files. You’ll use the Python [REPL](https://realpython.com/ref/glossary/repl/) along with a file named `sales_data_with_missing_values.csv`, which contains several null values you’ll deal with during the exercises. Before you start, extract this file from the downloadable materials by clicking the link at the end of this section. The `sales_data_with_missing_values.csv` file is based on the publicly available and complete [sales data](https://www.kaggle.com/datasets/satyamanidharv/sales-data-presentation-dashboards?resource=download) file from [Kaggle](https://www.kaggle.com/). Understanding the file’s content isn’t essential for this tutorial, but you can explore the Kaggle link above for more details if you’d like. You’ll also need to install both the pandas and PyArrow libraries to make sure all code examples work in your environment: - [Windows](https://realpython.com/how-to-drop-null-values-in-pandas/#windows-1) - [Linux + macOS](https://realpython.com/how-to-drop-null-values-in-pandas/#linux-macos-1) Windows PowerShell ``` PS> python -m pip install pandas pyarrow ``` Shell ``` $ python -m pip install pandas pyarrow ``` It’s time to refine your pandas skills by learning how to handle missing data in a variety of ways. You’ll find all code examples and the `sales_data_with_missing_values.csv` file in the materials for this tutorial, which you can download by clicking the link below: **Get Your Code:** [Click here to download the free sample code](https://realpython.com/bonus/how-to-drop-null-values-in-pandas-code/) that you’ll use to learn how to drop null values in pandas. ***Take the Quiz:*** Test your knowledge with our interactive “How to Drop Null Values in pandas” quiz. You’ll receive a score upon completion to help you track your learning progress: *** [![How to Drop Null Values in pandas](https://files.realpython.com/media/How-to-Drop-Null-Values-in-Pandas_Watermarked.f06e0d54bea4.jpg)](https://realpython.com/quizzes/how-to-drop-null-values-in-pandas/) **Interactive Quiz** [How to Drop Null Values in pandas](https://realpython.com/quizzes/how-to-drop-null-values-in-pandas/) Quiz yourself on pandas .dropna(): remove nulls, clean missing data, and prepare DataFrames for accurate analysis. ## How to Drop Rows Containing Null Values With pandas `.dropna()` Before you start dropping rows, it’s helpful to know what options `.dropna()` gives you. This method supports six **parameters** that let you control exactly what’s removed: - **`axis`**: Specifies whether to remove rows or columns containing null values. - **`thresh`** and **`how`**: Define how many missing values to remove or retain. - **`subset`**: Limits the removal of null values to specific parts of your DataFrame. - **`inplace`**: Determines whether the operation modifies the original DataFrame or returns a new copy. - **`ignore_index`**: Resets the DataFrame index after removing rows. Don’t worry if any of these parameters don’t make sense to you just yet—you’ll learn why each is used during this tutorial. You’ll also get the chance to practice your skills. **Note:** Although this tutorial teaches you how pandas DataFrames use `.dropna()`, DataFrames aren’t the only pandas objects that use it. [Series](https://pandas.pydata.org/docs/reference/series.html) objects also have their own [`.dropna()`](https://pandas.pydata.org/docs/reference/api/pandas.Series.dropna.html) method. However, the Series version contains only four parameters—`axis`, `inplace`, `how`, and `ignore_index`—instead of the six supported by the DataFrame version. Of these, only `inplace` and `ignore_index` are used, and they work the same way as in the DataFrame method. The rest are kept for compatibility with DataFrame, but have no effect. [Indexes](https://pandas.pydata.org/docs/reference/indexing.html) also have a [`.dropna()`](https://pandas.pydata.org/docs/reference/api/pandas.Index.dropna.html) method for removing missing index values, and it contains just one parameter: `how`. Before using `.dropna()` to drop rows, you should first find out whether your data contains any [null values](https://realpython.com/null-in-python/): Python ``` ``` To make sure all columns appear on your screen, you configure `pd.set_option("display.max_columns", None)`. By passing `None` as the second parameter, you make sure all columns are displayed. You read the `sales_data_with_missing_values.csv` file into a DataFrame using the [pandas `read_csv()`](https://realpython.com/pandas-read-write-files/#read-a-csv-file) function, then view the data. The order dates are in the `"%d/%m/%Y"` format in the file, so to make sure the `order_date` data is read correctly, you use both the `parse_dates` and `date_format` parameters. The output reveals there are ten rows and six columns of data in your file. **Note:** By default, pandas uses the [NumPy](https://realpython.com/numpy-tutorial/) library for its back-end data types. In the future—starting with [pandas 3](https://github.com/pandas-dev/pandas/milestone/102), which is under development—the default back end will be the more efficient [PyArrow](https://pandas.pydata.org/docs/user_guide/pyarrow.html). You can still get the benefits of this new back end and its [data types](https://arrow.apache.org/docs/python/api/datatypes) by configuring pandas using the `.convert_dtypes(dtype_backend="pyarrow")` method on your DataFrame. Using PyArrow also means the null values are displayed consistently as `<NA>`. If you use the default NumPy back end, then the null value in `order_date` would display as `NaT`, for *not a time*, while the rest would be shown as `NaN`, for *not a number*. Fortunately, `.dropna()` treats them all as null values. In a real [data analysis](https://realpython.com/python-for-data-analysis/), your DataFrame may be too large to allow you to see everything that’s missing. To solve this, you use the DataFrame’s [`.isna()`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.isna.html) and [`.sum()`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.sum.html) methods together: Python ``` ``` As you can see, each of your columns is missing data. When you use `sales_data.isna()`, you create a [Boolean](https://realpython.com/ref/glossary/boolean/) DataFrame the same size as `sales_data`, but with null values replaced with `True` and everything else replaced with `False`. The `.sum()` method counts each `True` and returns it to you as shown. **Note:** You may see [`.isnull()`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.isnull.html) being used. This is an alias for `.isna()`, and both work the same way. In addition, pandas provides [`.notna()`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.notna.html) and [`.notnull()`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.notnull.html), which you can use to reveal how many values in each column *don’t* contain nulls. To remove the rows with null values, you use [`.dropna()`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.dropna.html): Python ``` ``` As you can see, rows with null values are nowhere to be seen. You might think the rows containing the null values from `sales_data` have been deleted. After all, that’s what the output shows. However, this isn’t true. When you use `sales_data.dropna()`, the results are placed into a new DataFrame, leaving the original unchanged. If you want to retain this second DataFrame, then you need to assign the output from `.dropna()` to a new variable: Python ``` >>> clean_sales_data = sales_data.dropna() ``` Now the `clean_sales_data` DataFrame contains a second copy of the original `sales_data` DataFrame, but without those rows that contained one or more null values. Of course, this is wasteful of memory, so a better option would be to update the original DataFrame. To do this, you pass `inplace=True` to `.dropna()`: Python ``` >>> sales_data.dropna(inplace=True) ``` The original `sales_data` DataFrame will no longer contain rows with null values, and any future analysis will occur against this new version. **Note:** Even though `inplace=True` allows you to save memory, it offers little performance benefit during processing because a temporary copy is still made in the background. Also, applying some of your processing to your original DataFrame and some to a second copy can make finding errors difficult. A [pandas enhancement proposal (PDEP)](https://pandas.pydata.org/pdeps/0008-inplace-methods-in-pandas.html) calling for a deprecation was created because of these issues. Setting `inplace=True` is most useful when you want to make a one-time permanent change to your data and then use that version in all future analysis. You’ve now seen the basic use case for `.dropna()`, where you drop rows with at least one missing value. But the method also includes several other parameters worth exploring. [Remove ads](https://realpython.com/account/join/) ## How to Drop Columns Containing Null Values With pandas `.dropna()` So far, you’ve seen how to drop rows containing missing values, but it’s also possible to drop incomplete columns. You do this using the `axis` parameter. By default, `axis` is set to `0`, or `"index"`, which means it operates on rows of data. If you want to apply it across columns, then you set `axis` to `1` or `"columns"`. Suppose you want to remove **incomplete columns** containing null values from the original `sales_data`. Here’s how you do it: Python ``` ``` Because every column has at least one missing value, they’ve all been removed, leaving the DataFrame empty except for its index. So far, you’ve taken a fairly aggressive approach by removing entire rows and columns. Next, you’ll look at some less destructive alternatives. ## How to Work With a Part of Your Data Earlier, you used `.dropna()` to remove all null values from your DataFrame. While this is a common use case, you can also remove nulls from specific parts of the DataFrame or based on a certain quantity threshold. You’ll learn how in this section. ### Removing Data Based on Specific Rows or Columns You can restrict removal of data based on the rows or columns where the null values appear. This is where the `subset` parameter comes in handy. By passing a **column label** or a sequence of labels to `subset`, you tell `.dropna()` which columns to check for null values and remove their associated rows. Similarly, by passing `subset` an **index position** or a sequence of index positions and setting `axis="columns"`, you remove the column or columns that contain null values at the specified index positions. For example, suppose you want to remove rows containing null values in either the `discount` or `sale_price columns`. If there are null values in other columns besides `discount` or `sale_price`, then those rows will remain untouched. Here’s how you do it: Python ``` ``` This time, you’ve removed four rows. To clarify that you’re removing rows, you explicitly set `axis` to `0`. However, you can omit this argument since `0` is the default value for the `axis` parameter. To restrict your analysis to the two columns you want, you pass the Python list `["discount", "sale_price"]` as the `subset` parameter. ### Removing Data Based on the Quantity of Missing Values It’s possible to instruct `.dropna()` to remove rows or columns based on a specified quantity of data. Suppose you’re concerned about the volume of data missing in your DataFrame. You’re worried about rows with *more than* one missing value. In other words, you want to delete those rows containing at least five pieces of data in their six columns. To do this, you can set this threshold using the `thresh` parameter: Python ``` ``` Rows with indices `1` and `9` have been removed since they contain multiple missing values. By setting `thresh` to `5`, you tell `.dropna()` to keep all rows that contain at least five values. Since there are six columns, all remaining rows have no more than one missing value. ### Removing Empty Rows or Columns Sometimes you might want to remove only rows or columns that are completely empty, rather than those with just some missing values. You can control this with the `how` parameter. For example, suppose you want to remove rows where all values are missing: Python ``` ``` As you can see, the blank row at index position `9` has been removed. By passing `how="all"` to `.dropna()` and using the default value `axis=0`, you remove rows containing only null values. Alternatively, you could pass `how="any"` to remove rows or columns that have at least one null value. You rarely need to do this, however, because `"any"` is the default value. **Note:** The `how` and `thresh` parameters are mutually exclusive. In other words, you can use one or the other, but not both at the same time. It isn’t logical to specify `"any"` or `"all"` and then also set a threshold. [Remove ads](https://realpython.com/account/join/) ### Adding a Finishing Touch: Reset the Index Finally, you may have noticed that each time you remove a row—both here and throughout this tutorial—the index doesn’t update. As a finishing touch, passing `ignore_index=True` will reindex your DataFrame sequentially: Python ``` ``` This time, the index starts at `0` and ends at `8`, and there are only nine rows remaining. To wrap up what you’ve covered so far, it’s time to test your new skills. ## Practice Your pandas `.dropna()` Skills To practice, begin by loading the contents of `grades.csv` into a DataFrame: Python ``` ``` The DataFrame shows results for six students in five subjects. In this case, `<NA>` indicates that an exam wasn’t taken. Now, see if you can use `.dropna()` to answer each of the following questions: 1. Use `.dropna()` so that it permanently drops the row in the DataFrame containing only null values. Use this version from this point on. 2. Display the rows for the exams that all students completed. 3. Display any columns with no missing data. 4. Display the exams taken by at least five students. 5. Identify who else completed every exam that both S2 and S4 took. You’ll find the answers in the downloadable materials below: **Get Your Code:** [Click here to download the free sample code](https://realpython.com/bonus/how-to-drop-null-values-in-pandas-code/) that you’ll use to learn how to drop null values in pandas. ## Conclusion During this tutorial, you learned to use the DataFrame’s `.dropna()` method to remove null values from your DataFrames. **By using the various parameters, you’ve learned how:** - The `axis` parameter allows you to **remove rows or columns** containing null values. - The `thresh` and `how` parameters allow you to **define the quantity** of what gets removed. - The `subset` parameter lets you **restrict the removal** to part of your DataFrame. - The `inplace` parameter allows you to update either the **original DataFrame or a copy** after deletions. - The `ignore_index` parameter allows you to **[reset the DataFrame index](https://realpython.com/pandas-reset-index/)** after rows of data have been removed. Although you now have a solid understanding of `.dropna()`, there’s still lots to explore. Review what you’ve learned here to identify which techniques best fit your needs for handling missing values. You may also want to apply these skills to the `.dropna()` methods for pandas Series and Index objects. Doing so is a great way to continue your learning journey. ***Take the Quiz:*** Test your knowledge with our interactive “How to Drop Null Values in pandas” quiz. You’ll receive a score upon completion to help you track your learning progress: *** [![How to Drop Null Values in pandas](https://files.realpython.com/media/How-to-Drop-Null-Values-in-Pandas_Watermarked.f06e0d54bea4.jpg)](https://realpython.com/quizzes/how-to-drop-null-values-in-pandas/) **Interactive Quiz** [How to Drop Null Values in pandas](https://realpython.com/quizzes/how-to-drop-null-values-in-pandas/) Quiz yourself on pandas .dropna(): remove nulls, clean missing data, and prepare DataFrames for accurate analysis. [Remove ads](https://realpython.com/account/join/) ## Frequently Asked Questions Now that you have some experience with dropping null values in pandas, you can use the questions and answers below to check your understanding and recap what you’ve learned. These FAQs are related to the most important concepts you’ve covered in this tutorial. Click the *Show/Hide* toggle beside each question to reveal the answer. **How do you drop null values in a pandas DataFrame?**Show/Hide You use the `.dropna()` method to remove rows or columns with null values from your DataFrame. **How can you remove empty values in pandas?**Show/Hide You remove empty values by using the `.dropna()` method, which lets you drop rows or columns containing null values. **What is the `thresh` parameter used for in `.dropna()`?**Show/Hide You use the `thresh` parameter to specify the minimum number of non-null values a row or column must have to avoid being dropped. **How do you drop columns with missing data in pandas?**Show/Hide You set the `axis` parameter to `1` or `"columns"` in the `.dropna()` method to drop columns containing null values. **What happens when you set `inplace=True` in `.dropna()`?**Show/Hide When you set `inplace=True`, you update the original DataFrame directly, removing rows or columns with null values without creating a new DataFrame. Mark as Completed Share 🐍 Python Tricks 💌 Get a short & sweet **Python Trick** delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team. ![Python Tricks Dictionary Merge](https://realpython.com/static/pytrick-dict-merge.4201a0125a5e.png) About **Ian Eyre** [![Ian Eyre](https://realpython.com/cdn-cgi/image/width=644,height=644,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/Me_at_Graceland.f88418f34d62.fa6f5ab743da.png) ![Ian Eyre](https://realpython.com/cdn-cgi/image/width=644,height=644,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/Me_at_Graceland.f88418f34d62.fa6f5ab743da.png)](https://realpython.com/team/ieyre/) Ian is an avid Pythonista and Real Python contributor who loves to learn and teach others. [» More about Ian](https://realpython.com/team/ieyre/) *** *Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:* [![Aldren Santos](https://realpython.com/cdn-cgi/image/width=500,height=500,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/Aldren_Santos_Real_Python.6b0861d8b841.png)](https://realpython.com/team/asantos/) [Aldren](https://realpython.com/team/asantos/) [![Brenda Weleschuk](https://realpython.com/cdn-cgi/image/width=320,height=320,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/IMG_3324_1.50b309355fc1.jpg)](https://realpython.com/team/bweleschuk/) [Brenda](https://realpython.com/team/bweleschuk/) [![Bartosz ZaczyƄski](https://realpython.com/cdn-cgi/image/width=1694,height=1694,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/coders_lab_2109368.259b1599fbee.jpg)](https://realpython.com/team/bzaczynski/) [Bartosz](https://realpython.com/team/bzaczynski/) [![Stephen Gruppetta](https://realpython.com/cdn-cgi/image/width=400,height=400,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/Stephen_inside_2_BW_2_square_crop_2_low_res_2_copy.4a7e2d8bc19c.png)](https://realpython.com/team/sgruppetta/) [Stephen](https://realpython.com/team/sgruppetta/) Master Real-World Python Skills With Unlimited Access to Real Python ![Locked learning resources](https://realpython.com/static/videos/lesson-locked.f5105cfd26db.svg) **Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:** [Level Up Your Python Skills »](https://realpython.com/account/join/?utm_source=rp_article_footer&utm_content=how-to-drop-null-values-in-pandas) Master Real-World Python Skills With Unlimited Access to Real Python ![Locked learning resources](https://realpython.com/static/videos/lesson-locked.f5105cfd26db.svg) **Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:** [Level Up Your Python Skills »](https://realpython.com/account/join/?utm_source=rp_article_footer&utm_content=how-to-drop-null-values-in-pandas) What Do You Think? **Rate this article:** [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Frealpython.com%2Fhow-to-drop-null-values-in-pandas%2F) [Twitter](https://twitter.com/intent/tweet/?text=Interesting%20Python%20article%20on%20%40realpython%3A%20How%20to%20Drop%20Null%20Values%20in%20pandas%20With%20.dropna%28%29&url=https%3A%2F%2Frealpython.com%2Fhow-to-drop-null-values-in-pandas%2F) [Bluesky](https://bsky.app/intent/compose?text=Interesting%20Python%20article%20on%20%40realpython.com%3A%20How%20to%20Drop%20Null%20Values%20in%20pandas%20With%20.dropna%28%29%20https%3A%2F%2Frealpython.com%2Fhow-to-drop-null-values-in-pandas%2F) [Facebook](https://facebook.com/sharer/sharer.php?u=https%3A%2F%2Frealpython.com%2Fhow-to-drop-null-values-in-pandas%2F) [Email](mailto:?subject=Python%20article%20for%20you&body=How%20to%20Drop%20Null%20Values%20in%20pandas%20With%20.dropna%28%29%20on%20Real%20Python%0A%0Ahttps%3A%2F%2Frealpython.com%2Fhow-to-drop-null-values-in-pandas%2F%0A) What’s your \#1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know. **Commenting Tips:** The most useful comments are those written with the goal of learning from or helping out other students. [Get tips for asking good questions](https://realpython.com/python-beginner-tips/#tip-9-ask-good-questions) and [get answers to common questions in our support portal](https://support.realpython.com/). *** Looking for a real-time conversation? Visit the [Real Python Community Chat](https://realpython.com/community/) or join the next [“Office Hours” Live Q\&A Session](https://realpython.com/office-hours/). Happy Pythoning\! Keep Learning Related Topics: [basics](https://realpython.com/tutorials/basics/) [data-science](https://realpython.com/tutorials/data-science/) [python](https://realpython.com/tutorials/python/) ## Keep reading Real Python by creating a free account or signing in: [![Keep reading](https://realpython.com/static/videos/lesson-locked.f5105cfd26db.svg)](https://realpython.com/account/signup/?intent=continue_reading&utm_source=rp&utm_medium=web&utm_campaign=rwn&utm_content=v1&next=%2Fhow-to-drop-null-values-in-pandas%2F) [Continue »](https://realpython.com/account/signup/?intent=continue_reading&utm_source=rp&utm_medium=web&utm_campaign=rwn&utm_content=v1&next=%2Fhow-to-drop-null-values-in-pandas%2F) Already have an account? [Sign-In](https://realpython.com/account/login/?next=/how-to-drop-null-values-in-pandas/) Almost there! Complete this form and click the button below to gain instant access: × ![How to Drop Null Values in pandas](https://files.realpython.com/media/How-to-Drop-Null-Values-in-Pandas_Watermarked.f06e0d54bea4.jpg) How to Drop Null Values in pandas (Sample Code) ##### Learn Python - [Start Here](https://realpython.com/start-here/) - [Learning Resources](https://realpython.com/search) - [Code Mentor](https://realpython.com/mentor/) - [Python Reference](https://realpython.com/ref/) - [Python Cheat Sheet](https://realpython.com/cheatsheets/python/) - [Support Center](https://support.realpython.com/) ##### Courses & Paths - [Learning Paths](https://realpython.com/learning-paths/) - [Quizzes & Exercises](https://realpython.com/quizzes/) - [Browse Topics](https://realpython.com/tutorials/all/) - [Live Courses](https://realpython.com/live/) - [Books](https://realpython.com/books/) ##### Community - [Podcast](https://realpython.com/podcasts/rpp/) - [Newsletter](https://realpython.com/newsletter/) - [Community Chat](https://realpython.com/community/) - [Office Hours](https://realpython.com/office-hours/) - [Learner Stories](https://realpython.com/learner-stories/) ##### Membership - [Plans & Pricing](https://realpython.com/account/join/) - [Team Plans](https://realpython.com/account/join-team/) - [For Business](https://realpython.com/account/join-team/inquiry/) - [For Schools](https://realpython.com/account/join-team/education-inquiry/) - [Reviews](https://realpython.com/learner-stories/) ##### Company - [About Us](https://realpython.com/about/) - [Team](https://realpython.com/team/) - [Mission & Values](https://realpython.com/mission/) - [Editorial Guidelines](https://realpython.com/editorial-guidelines/) - [Sponsorships](https://realpython.com/sponsorships/) - [Careers](https://realpython.workable.com/) - [Press Kit](https://realpython.com/media-kit/) - [Merch](https://realpython.com/merch) [Privacy Policy](https://realpython.com/privacy-policy/) ⋅ [Terms of Use](https://realpython.com/terms/) ⋅ [Security](https://realpython.com/security/) ⋅ [Contact](https://realpython.com/contact/) Happy Pythoning\! © 2012–2026 DevCademy Media Inc. DBA Real Python. All rights reserved. REALPYTHONℱ is a trademark of DevCademy Media Inc. [![Real Python - Online Python Training (logo)](https://realpython.com/static/real-python-logo-primary.973743b6d39d.svg)](https://realpython.com/) ![](https://www.facebook.com/tr?id=2220911568135371&ev=PageView&noscript=1) You've blocked notifications
Readable Markdown
by [Ian Eyre](https://realpython.com/how-to-drop-null-values-in-pandas/#author) Publication date Sep 10, 2025 Reading time estimate 18m [basics](https://realpython.com/tutorials/basics/) [data-science](https://realpython.com/tutorials/data-science/) [python](https://realpython.com/tutorials/python/) Missing values can derail your analysis. In pandas, you can use the `.dropna()` method to remove rows or columns containing null values—in other words, missing data—so you can work with clean DataFrames. In this tutorial, you’ll learn how this method’s parameters let you control exactly which data gets removed. As you’ll see, these parameters give you fine-grained control over how much of your data to clean. Dealing with null values is essential for keeping datasets clean and avoiding the issues they can cause. Missing entries can lead to misinterpreted column data types, inaccurate conclusions, and errors in calculations. Simply put, nulls can cause havoc if they find their way into your calculations. **By the end of this tutorial, you’ll understand that:** - You can use **`.dropna()`** to **remove rows and columns** from a pandas DataFrame. - You can remove rows and columns based on the **content of a subset** of your DataFrame. - You can remove rows and columns based on the **volume of null values** within your DataFrame. To get the most out of this tutorial, it’s recommended that you already have a basic understanding of how to create [pandas DataFrames](https://realpython.com/pandas-dataframe/) from files. You’ll use the Python [REPL](https://realpython.com/ref/glossary/repl/) along with a file named `sales_data_with_missing_values.csv`, which contains several null values you’ll deal with during the exercises. Before you start, extract this file from the downloadable materials by clicking the link at the end of this section. The `sales_data_with_missing_values.csv` file is based on the publicly available and complete [sales data](https://www.kaggle.com/datasets/satyamanidharv/sales-data-presentation-dashboards?resource=download) file from [Kaggle](https://www.kaggle.com/). Understanding the file’s content isn’t essential for this tutorial, but you can explore the Kaggle link above for more details if you’d like. You’ll also need to install both the pandas and PyArrow libraries to make sure all code examples work in your environment: - [Windows](https://realpython.com/how-to-drop-null-values-in-pandas/#windows-1) - [Linux + macOS](https://realpython.com/how-to-drop-null-values-in-pandas/#linux-macos-1) It’s time to refine your pandas skills by learning how to handle missing data in a variety of ways. You’ll find all code examples and the `sales_data_with_missing_values.csv` file in the materials for this tutorial, which you can download by clicking the link below: ***Take the Quiz:*** Test your knowledge with our interactive “How to Drop Null Values in pandas” quiz. You’ll receive a score upon completion to help you track your learning progress: *** [![How to Drop Null Values in pandas](https://files.realpython.com/media/How-to-Drop-Null-Values-in-Pandas_Watermarked.f06e0d54bea4.jpg)](https://realpython.com/quizzes/how-to-drop-null-values-in-pandas/) ## How to Drop Rows Containing Null Values With pandas `.dropna()` Before you start dropping rows, it’s helpful to know what options `.dropna()` gives you. This method supports six **parameters** that let you control exactly what’s removed: - **`axis`**: Specifies whether to remove rows or columns containing null values. - **`thresh`** and **`how`**: Define how many missing values to remove or retain. - **`subset`**: Limits the removal of null values to specific parts of your DataFrame. - **`inplace`**: Determines whether the operation modifies the original DataFrame or returns a new copy. - **`ignore_index`**: Resets the DataFrame index after removing rows. Don’t worry if any of these parameters don’t make sense to you just yet—you’ll learn why each is used during this tutorial. You’ll also get the chance to practice your skills. Before using `.dropna()` to drop rows, you should first find out whether your data contains any [null values](https://realpython.com/null-in-python/): To make sure all columns appear on your screen, you configure `pd.set_option("display.max_columns", None)`. By passing `None` as the second parameter, you make sure all columns are displayed. You read the `sales_data_with_missing_values.csv` file into a DataFrame using the [pandas `read_csv()`](https://realpython.com/pandas-read-write-files/#read-a-csv-file) function, then view the data. The order dates are in the `"%d/%m/%Y"` format in the file, so to make sure the `order_date` data is read correctly, you use both the `parse_dates` and `date_format` parameters. The output reveals there are ten rows and six columns of data in your file. In a real [data analysis](https://realpython.com/python-for-data-analysis/), your DataFrame may be too large to allow you to see everything that’s missing. To solve this, you use the DataFrame’s [`.isna()`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.isna.html) and [`.sum()`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.sum.html) methods together: As you can see, each of your columns is missing data. When you use `sales_data.isna()`, you create a [Boolean](https://realpython.com/ref/glossary/boolean/) DataFrame the same size as `sales_data`, but with null values replaced with `True` and everything else replaced with `False`. The `.sum()` method counts each `True` and returns it to you as shown. To remove the rows with null values, you use [`.dropna()`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.dropna.html): As you can see, rows with null values are nowhere to be seen. You might think the rows containing the null values from `sales_data` have been deleted. After all, that’s what the output shows. However, this isn’t true. When you use `sales_data.dropna()`, the results are placed into a new DataFrame, leaving the original unchanged. If you want to retain this second DataFrame, then you need to assign the output from `.dropna()` to a new variable: Now the `clean_sales_data` DataFrame contains a second copy of the original `sales_data` DataFrame, but without those rows that contained one or more null values. Of course, this is wasteful of memory, so a better option would be to update the original DataFrame. To do this, you pass `inplace=True` to `.dropna()`: The original `sales_data` DataFrame will no longer contain rows with null values, and any future analysis will occur against this new version. You’ve now seen the basic use case for `.dropna()`, where you drop rows with at least one missing value. But the method also includes several other parameters worth exploring. ## How to Drop Columns Containing Null Values With pandas `.dropna()` So far, you’ve seen how to drop rows containing missing values, but it’s also possible to drop incomplete columns. You do this using the `axis` parameter. By default, `axis` is set to `0`, or `"index"`, which means it operates on rows of data. If you want to apply it across columns, then you set `axis` to `1` or `"columns"`. Suppose you want to remove **incomplete columns** containing null values from the original `sales_data`. Here’s how you do it: Because every column has at least one missing value, they’ve all been removed, leaving the DataFrame empty except for its index. So far, you’ve taken a fairly aggressive approach by removing entire rows and columns. Next, you’ll look at some less destructive alternatives. ## How to Work With a Part of Your Data Earlier, you used `.dropna()` to remove all null values from your DataFrame. While this is a common use case, you can also remove nulls from specific parts of the DataFrame or based on a certain quantity threshold. You’ll learn how in this section. ### Removing Data Based on Specific Rows or Columns You can restrict removal of data based on the rows or columns where the null values appear. This is where the `subset` parameter comes in handy. By passing a **column label** or a sequence of labels to `subset`, you tell `.dropna()` which columns to check for null values and remove their associated rows. Similarly, by passing `subset` an **index position** or a sequence of index positions and setting `axis="columns"`, you remove the column or columns that contain null values at the specified index positions. For example, suppose you want to remove rows containing null values in either the `discount` or `sale_price columns`. If there are null values in other columns besides `discount` or `sale_price`, then those rows will remain untouched. Here’s how you do it: This time, you’ve removed four rows. To clarify that you’re removing rows, you explicitly set `axis` to `0`. However, you can omit this argument since `0` is the default value for the `axis` parameter. To restrict your analysis to the two columns you want, you pass the Python list `["discount", "sale_price"]` as the `subset` parameter. ### Removing Data Based on the Quantity of Missing Values It’s possible to instruct `.dropna()` to remove rows or columns based on a specified quantity of data. Suppose you’re concerned about the volume of data missing in your DataFrame. You’re worried about rows with *more than* one missing value. In other words, you want to delete those rows containing at least five pieces of data in their six columns. To do this, you can set this threshold using the `thresh` parameter: Rows with indices `1` and `9` have been removed since they contain multiple missing values. By setting `thresh` to `5`, you tell `.dropna()` to keep all rows that contain at least five values. Since there are six columns, all remaining rows have no more than one missing value. ### Removing Empty Rows or Columns Sometimes you might want to remove only rows or columns that are completely empty, rather than those with just some missing values. You can control this with the `how` parameter. For example, suppose you want to remove rows where all values are missing: As you can see, the blank row at index position `9` has been removed. By passing `how="all"` to `.dropna()` and using the default value `axis=0`, you remove rows containing only null values. Alternatively, you could pass `how="any"` to remove rows or columns that have at least one null value. You rarely need to do this, however, because `"any"` is the default value. ### Adding a Finishing Touch: Reset the Index Finally, you may have noticed that each time you remove a row—both here and throughout this tutorial—the index doesn’t update. As a finishing touch, passing `ignore_index=True` will reindex your DataFrame sequentially: This time, the index starts at `0` and ends at `8`, and there are only nine rows remaining. To wrap up what you’ve covered so far, it’s time to test your new skills. ## Practice Your pandas `.dropna()` Skills To practice, begin by loading the contents of `grades.csv` into a DataFrame: The DataFrame shows results for six students in five subjects. In this case, `<NA>` indicates that an exam wasn’t taken. Now, see if you can use `.dropna()` to answer each of the following questions: 1. Use `.dropna()` so that it permanently drops the row in the DataFrame containing only null values. Use this version from this point on. 2. Display the rows for the exams that all students completed. 3. Display any columns with no missing data. 4. Display the exams taken by at least five students. 5. Identify who else completed every exam that both S2 and S4 took. You’ll find the answers in the downloadable materials below: ## Conclusion During this tutorial, you learned to use the DataFrame’s `.dropna()` method to remove null values from your DataFrames. **By using the various parameters, you’ve learned how:** - The `axis` parameter allows you to **remove rows or columns** containing null values. - The `thresh` and `how` parameters allow you to **define the quantity** of what gets removed. - The `subset` parameter lets you **restrict the removal** to part of your DataFrame. - The `inplace` parameter allows you to update either the **original DataFrame or a copy** after deletions. - The `ignore_index` parameter allows you to **[reset the DataFrame index](https://realpython.com/pandas-reset-index/)** after rows of data have been removed. Although you now have a solid understanding of `.dropna()`, there’s still lots to explore. Review what you’ve learned here to identify which techniques best fit your needs for handling missing values. You may also want to apply these skills to the `.dropna()` methods for pandas Series and Index objects. Doing so is a great way to continue your learning journey. ***Take the Quiz:*** Test your knowledge with our interactive “How to Drop Null Values in pandas” quiz. You’ll receive a score upon completion to help you track your learning progress: *** [![How to Drop Null Values in pandas](https://files.realpython.com/media/How-to-Drop-Null-Values-in-Pandas_Watermarked.f06e0d54bea4.jpg)](https://realpython.com/quizzes/how-to-drop-null-values-in-pandas/) ## Frequently Asked Questions Now that you have some experience with dropping null values in pandas, you can use the questions and answers below to check your understanding and recap what you’ve learned. These FAQs are related to the most important concepts you’ve covered in this tutorial. Click the *Show/Hide* toggle beside each question to reveal the answer. You use the `.dropna()` method to remove rows or columns with null values from your DataFrame. You remove empty values by using the `.dropna()` method, which lets you drop rows or columns containing null values. You use the `thresh` parameter to specify the minimum number of non-null values a row or column must have to avoid being dropped. You set the `axis` parameter to `1` or `"columns"` in the `.dropna()` method to drop columns containing null values. When you set `inplace=True`, you update the original DataFrame directly, removing rows or columns with null values without creating a new DataFrame.
Shard71 (laksa)
Root Hash13351397557425671
Unparsed URLcom,realpython!/how-to-drop-null-values-in-pandas/ s443