âčïž Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.2 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://sparkbyexamples.com/pandas/pandas-drop-rows-with-nan-values-in-dataframe/ |
| Last Crawled | 2026-04-01 12:42:16 (5 days ago) |
| First Indexed | 2021-09-22 15:35:01 (4 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Pandas Drop Rows with NaN Values in DataFrame - Spark By {Examples} |
| Meta Description | To drop rows with NaN (null) values in a Pandas DataFrame, you can use the dropna() function. Python doesn't support Null hence any missing data is |
| Meta Canonical | null |
| Boilerpipe Text | To drop rows with NaN (null) values in a Pandas DataFrame, you can use the
dropna()
function. Python doesnât support Null hence any missing data is represented as None or NaN. NaN stands for Not A Number and serves as one of the typical methods for indicating
missing values
within datasets. None/NaN values are one of the major problems in Data Analysis hence before we process either you need to drop rows that have NaN values or
replace NaN with empty for Strings
and
replace NaN with zero for numeric columns
.
Advertisements
Key Points â
Use the
dropna()
function in Pandas to remove rows containing
NaN/None
values from a DataFrame.
numpy.nan
 is
Not a Number (NaN)
, which is of Python build-in numeric type float (floating point).
None
 is ofÂ
NoneTypeÂ
and it is an object in Python.
Specify the axis parameter as 0 to drop rows with NaN values.
The
dropna()
function returns a new DataFrame with NaN-containing rows removed.
Use additional parameters like
subset
to specify columns to consider for NaN removal, and
how
to control the criteria for dropping rows.
Quick Examples of Dropping Rows with NaN Values
Below are quick examples of dropping rows with nan values.
# Quick examples of drop rows with nan values
# Example 1: Drop all rows with NaN values
df2=df.dropna()
df2=df.dropna(axis=0)
# Example 2: Reset index after drop
df2=df.dropna().reset_index(drop=True)
# Example 3: Drop row that has all NaN values
df2=df.dropna(how='all')
# Example 4: Drop rows that has NaN values on selected columns
df2=df.dropna(subset=['Courses','Fee'])
# Example 5: With threshold,
# Keep only the rows with at least 2 non-NA values.
df2=df.dropna(thresh=2)
# Example 6: Drop Rows with NaN Values inplace
df.dropna(inplace=True)
In real-world datasets, missing values are quite common. To remove all rows containing any NaN values in a Pandas DataFrame, you can simply use the
dropna()
method. This provides an easy way to clean your data by filtering out incomplete rows.
To run some examples of drop rows with NaN values in Pandas DataFrame, letâs create a Pandas DataFrame using data from a dictionary.
import pandas as pd
import numpy as np
technologies = ({
'Courses':["Spark",'Java',"Hadoop",'Python',np.nan],
'Fee' :[20000,np.nan,26000,24000,np.nan],
'Duration':['30days',np.nan,'35days','40days',np.nan],
'Discount':[1000,np.nan,2500,None,np.nan]
})
df = pd.DataFrame(technologies)
print("Create DataFrame:\n", df)
Yields below output.
Drop Rows with NaN Values
You can use the
dropna()
method to remove rows with NaN (Not a Number) and None values from Pandas DataFrame. By default, it removes any row containing at least one NaN value and returns the copy of the DataFrame after removing rows. If you want to remove from the existing DataFrame, you should use
inplace=True
.
With NaN values in a Pandas DataFrame.
# Drop all rows that have NaN/None values
df2 = df.dropna()
print("After dropping the rows with NaN Values:\n", df2)
Yields below output.
Related:
you can use the
dropna(axis=1)
to
drop all columns with NaN values
from DataFrame.
Post-dropping rows with NaN, sometimes you may be required to reset the index, you can do so using the
DataFrame.reset_index()
method.
# Reset index after drop
df2 = df.dropna().reset_index(drop=True)
print("Reset the index after dropping:\n", df2)
Yields below output.
# Output:
# Reset the index after dropping
Courses Fee Duration Discount
0 Spark 20000.0 30days 1000.0
1 Hadoop 26000.0 35days 2500.0
Drop NaNs for all Columns in the DataFrame
Similarly, you can use
how
parameter of the
dropna()
function to specify which rows to drop based on NaN values. By default, the Param
how=any
specifies all rows with NaN/None values on any element are removed.
You can use
how='all'
to remove rows that have all NaN/None values in a row(data is missing for all elements in a row).
# Drop rows that has all NaN values
df2 = df.dropna(how='all')
print(" After dropping the rows which have all NaN values:\n", df2)
Yields below output.
# Output:
# After dropping the rows which have all NaN values:
Courses Fee Duration Discount
0 Spark 20000.0 30days 1000.0
1 Java NaN NaN NaN
2 Hadoop 26000.0 35days 2500.0
3 Python 24000.0 40days NaN
Drop NaN Values on Selected Columns from List
Sometimes you may be required to drop rows only when selected columns have NaN/None values in DataFrame, you can achieve this by using
subset
param. This parameter takes a list of label names.
# Drop rows that has NaN values on selected columns
df2=df.dropna(subset=['Courses','Fee'])
print("After dropping rows based on specified columns:\n", df2)
Yields below output.
# Output:
# After dropping rows based on specified columns:
Courses Fee Duration Discount
0 Spark 20000.0 30days 1000.0
2 Hadoop 26000.0 35days 2500.0
3 Python 24000.0 40days NaN
Drop Rows with NaN Values inplace
As you can see, by default
dropna()
method doesnât drop rows from the original DataFrame; instead, it returns a copy of the DataFrame. If you intend to modify the existing DataFrame directly, you can set
inplace=True
.
# Drop Rows with NaN Values inplace
df.dropna(inplace=True)
print("After dropping the rows with NaN values:\n", df)
# Output:
# After dropping the rows with NaN values:
# Courses Fee Duration Discount
# 0 Spark 20000.0 30days 1000.0
# 2 Hadoop 26000.0 35days 2500.0
Complete Example of Drop Rows with NaN Values
import pandas as pd
import numpy as np
technologies = ({
'Courses':["Spark",'Java',"Hadoop",'Python',np.nan],
'Fee' :[20000,np.nan,26000,24000,np.nan],
'Duration':['30days',np.nan,'35days','40days',np.nan],
'Discount':[1000,np.nan,2500,None,np.nan]
})
df = pd.DataFrame(technologies)
print(df)
# Drop all rows with NaN values
df2=df.dropna()
print(df2)
df2=df.dropna(axis=0)
# Reset index after drop
df2=df.dropna().reset_index(drop=True)
print(df2)
# Drop row that has all NaN values
df2=df.dropna(how='all')
print(df2)
# Drop rows that has null on selected columns
df2=df.dropna(subset=['Courses','Fee'])
print(df2)
# Drop Rows with NaN Values inplace
df.dropna(inplace=True)
print(df)
FAQ on Drop Rows with NaN Values
How do I drop rows with NaN values in a Pandas DataFrame?
You can use the
dropna()
method to remove rows with NaN values in a Pandas DataFrame. By default, it removes any of the rows having at least one NaN value. For instance,
df.dropna()
.
What is the syntax for using the dropna() function to remove rows with NaN values?
df.dropna()
which returns a new DataFrame with rows that donât have NaN values.
How can I drop rows with NaN values in a specific column?
You can use the
subset
parameter of the
dropna()
method to specify a subset of columns to consider for NaN removal. For example,
df.dropna(subset=['specified_column'])
How can I drop rows based on multiple columns with NaN values?
You can specify multiple columns in the
subset
parameter. For example,
df.dropna(subset=['specified_column1', 'specified_column2'])
How can I drop rows if all values in a row are NaN?
You can use the
how
parameter with the value âallâ to drop rows where all values are NaN. For example,
df.dropna(how='all')
Conclusion
In this article, I have explained dropping rows with NaN/None values in pandas DataFrame using
DataFrame.dropna()
. Also learned to remove rows only when all values are NaN/None, remove only when selected columns have NaN values, and remove using the
inplace
parameter.
Happy Learning !!
Related Articles
Pandas Drop Rows by Index
Pandas â Drop the First Three Rows
Drop Pandas rows based on condition
Delete Last Row From Pandas DataFrame
Pandas â Drop List of Rows From DataFrame
Pandas Drop Last N Rows From DataFrame
How to drop duplicate rows from DataFrame?
Pandas Drop Rows Based on Column Value
References
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.dropna.html? |
| Markdown | [Skip to content](https://sparkbyexamples.com/pandas/pandas-drop-rows-with-nan-values-in-dataframe/#main)
- [Home](https://sparkbyexamples.com/)
- [About](https://sparkbyexamples.com/about-sparkbyexamples/)
\| \*\*\* Please
[**Subscribe**](https://sparkbyexamples.com/membership-account/membership-levels)
for Ad Free & Premium Content \*\*\*
[Spark By {Examples}](https://sparkbyexamples.com/)
- [Connect](https://topmate.io/naveennel)
- [\|](https://sparkbyexamples.com/pandas/pandas-drop-rows-with-nan-values-in-dataframe/)
- [Join for Ad Free](https://sparkbyexamples.com/membership-account/membership-levels/)
- [Courses](https://sparkbyexamples.thinkific.com/courses/)
- [Mastering Spark with Scala](https://sparkbyexamples.thinkific.com/courses/mastering-apache-spark-tutorial)
- [Mastering PySpark](https://sparkbyexamples.thinkific.com/courses/pyspark-tutorial)
- [Spark](https://sparkbyexamples.com/ "Apache Spark")
- [Spark Introduction](https://sparkbyexamples.com/)
- [Spark RDD Tutorial](https://sparkbyexamples.com/spark-rdd-tutorial/)
- [Spark SQL Functions](https://sparkbyexamples.com/spark/spark-sql-functions/)
- [Whatâs New in Spark 3.0?](https://sparkbyexamples.com/category/spark/spark-3-0/)
- [Spark Streaming](https://sparkbyexamples.com/apache-spark-streaming-tutorial/)
- [Apache Spark on AWS](https://sparkbyexamples.com/apache-spark-on-amazon-web-services/)
- [Apache Spark Interview Questions](https://sparkbyexamples.com/interview-questions/apache-spark-interview-questions/)
- [PySpark](https://sparkbyexamples.com/pyspark-tutorial/)
- [Pandas](https://sparkbyexamples.com/python-pandas-tutorial-for-beginners/)
- [R](https://sparkbyexamples.com/r-tutorial-with-examples/)
- [R Programming](https://sparkbyexamples.com/r-tutorial-with-examples/)
- [R Data Frame](https://sparkbyexamples.com/r-programming/r-data-frames/)
- [R dplyr Tutorial](https://sparkbyexamples.com/r-programming/r-dplyr-tutorial-learn-with-examples/)
- [R Vector](https://sparkbyexamples.com/r-programming/vector-in-r/)
- [Tutorials](https://sparkbyexamples.com/)
- [Hive](https://sparkbyexamples.com/apache-hive-tutorial/)
- [Snowflake](https://sparkbyexamples.com/snowflake-data-warehouse-database-tutorials/)
- [H2O.ai](https://sparkbyexamples.com/h2o-sparkling-water-tutorial-beginners/)
- [AWS](https://sparkbyexamples.com/apache-spark-on-amazon-web-services/)
- [Apache Kafka Tutorials with Examples](https://sparkbyexamples.com/apache-kafka-tutorials-with-examples/)
- [Apache Hadoop Tutorials with Examples :](https://sparkbyexamples.com/apache-hadoop-tutorials-with-examples/)
- [NumPy](https://sparkbyexamples.com/python-numpy-tutorial-for-beginners/)
- [Apache HBase](https://sparkbyexamples.com/apache-hbase-tutorial/)
- [Apache Cassandra Tutorials with Examples](https://sparkbyexamples.com/apache-cassandra-tutorials-with-examples/)
- [H2O Sparkling Water](https://sparkbyexamples.com/h2o-sparkling-water-tutorial-beginners/)
- [Pricing](https://sparkbyexamples.com/membership-account/membership-levels/)
- [Log In](https://sparkbyexamples.com/login/)
- [Toggle website search](https://sparkbyexamples.com/)
[Menu Close](https://sparkbyexamples.com/#mobile-menu-toggle)
- [Courses](https://sparkbyexamples.thinkific.com/courses/)
- [Mastering Spark with Scala](https://sparkbyexamples.thinkific.com/courses/mastering-apache-spark-tutorial)
- [Mastering PySpark](https://sparkbyexamples.thinkific.com/courses/pyspark-tutorial)
- [Spark](https://sparkbyexamples.com/ "Apache Spark")
- [Spark Introduction](https://sparkbyexamples.com/)
- [Spark RDD Tutorial](https://sparkbyexamples.com/spark-rdd-tutorial/)
- [Spark SQL Functions](https://sparkbyexamples.com/spark/spark-sql-functions/)
- [Whatâs New in Spark 3.0?](https://sparkbyexamples.com/category/spark/spark-3-0/)
- [Spark Streaming](https://sparkbyexamples.com/apache-spark-streaming-tutorial/)
- [Apache Spark on AWS](https://sparkbyexamples.com/apache-spark-on-amazon-web-services/)
- [Apache Spark Interview Questions](https://sparkbyexamples.com/interview-questions/apache-spark-interview-questions/)
- [PySpark](https://sparkbyexamples.com/pyspark-tutorial/)
- [Pandas](https://sparkbyexamples.com/python-pandas-tutorial-for-beginners/)
- [R](https://sparkbyexamples.com/r-tutorial-with-examples/)
- [R Programming](https://sparkbyexamples.com/r-tutorial-with-examples/)
- [R Data Frame](https://sparkbyexamples.com/r-programming/r-data-frames/)
- [R dplyr Tutorial](https://sparkbyexamples.com/r-programming/r-dplyr-tutorial-learn-with-examples/)
- [R Vector](https://sparkbyexamples.com/r-programming/vector-in-r/)
- [Tutorials](https://sparkbyexamples.com/)
- [Hive](https://sparkbyexamples.com/apache-hive-tutorial/)
- [Snowflake](https://sparkbyexamples.com/snowflake-data-warehouse-database-tutorials/)
- [H2O.ai](https://sparkbyexamples.com/h2o-sparkling-water-tutorial-beginners/)
- [AWS](https://sparkbyexamples.com/apache-spark-on-amazon-web-services/)
- [Apache Kafka Tutorials with Examples](https://sparkbyexamples.com/apache-kafka-tutorials-with-examples/)
- [Apache Hadoop Tutorials with Examples :](https://sparkbyexamples.com/apache-hadoop-tutorials-with-examples/)
- [NumPy](https://sparkbyexamples.com/python-numpy-tutorial-for-beginners/)
- [Apache HBase](https://sparkbyexamples.com/apache-hbase-tutorial/)
- [Apache Cassandra Tutorials with Examples](https://sparkbyexamples.com/apache-cassandra-tutorials-with-examples/)
- [H2O Sparkling Water](https://sparkbyexamples.com/h2o-sparkling-water-tutorial-beginners/)
- [Pricing](https://sparkbyexamples.com/membership-account/membership-levels/)
- [Log In](https://sparkbyexamples.com/login/)
- [Toggle website search](https://sparkbyexamples.com/)
- [Home](https://sparkbyexamples.com/)
- [About](https://sparkbyexamples.com/about-sparkbyexamples/)
# Pandas Drop Rows with NaN Values in DataFrame
[Home](https://sparkbyexamples.com/) » [Pandas](https://sparkbyexamples.com/category/pandas/) » Pandas Drop Rows with NaN Values in DataFrame
- Post author:[Naveen Nelamali](https://sparkbyexamples.com/author/naveen/ "Posts by Naveen Nelamali")
- Post category:[Pandas](https://sparkbyexamples.com/category/pandas/)
- Post last modified:June 19, 2025
- Reading time:15 mins read

To drop rows with NaN (null) values in a Pandas DataFrame, you can use the `dropna()` function. Python doesnât support Null hence any missing data is represented as None or NaN. NaN stands for Not A Number and serves as one of the typical methods for indicating [missing values](https://sparkbyexamples.com/pandas/pandas-handle-missing-data-in-dataframe/) within datasets. None/NaN values are one of the major problems in Data Analysis hence before we process either you need to drop rows that have NaN values or [replace NaN with empty for Strings](https://sparkbyexamples.com/pandas/pandas-replace-nan-with-blank-empty-string/) and [replace NaN with zero for numeric columns](https://sparkbyexamples.com/pandas/pandas-replace-nan-values-by-zeroes-in-a-column/).
Advertisements
****Key Points â****
- Use the [dropna()](https://sparkbyexamples.com/pandas/pandas-dropna-usage-examples/) function in Pandas to remove rows containing `NaN/None` values from a DataFrame.
- `numpy.nan` is [Not a Number (NaN)](https://numpy.org/devdocs/reference/constants.html), which is of Python build-in numeric type float (floating point).
- `None` is of [NoneType](https://docs.python.org/2/library/types.html) and it is an object in Python.
- Specify the axis parameter as 0 to drop rows with NaN values.
- The `dropna()` function returns a new DataFrame with NaN-containing rows removed.
- Use additional parameters like `subset` to specify columns to consider for NaN removal, and `how` to control the criteria for dropping rows.
## Quick Examples of Dropping Rows with NaN Values
Below are quick examples of dropping rows with nan values.
```
# Quick examples of drop rows with nan values
# Example 1: Drop all rows with NaN values
df2=df.dropna()
df2=df.dropna(axis=0)
# Example 2: Reset index after drop
df2=df.dropna().reset_index(drop=True)
# Example 3: Drop row that has all NaN values
df2=df.dropna(how='all')
# Example 4: Drop rows that has NaN values on selected columns
df2=df.dropna(subset=['Courses','Fee'])
# Example 5: With threshold,
# Keep only the rows with at least 2 non-NA values.
df2=df.dropna(thresh=2)
# Example 6: Drop Rows with NaN Values inplace
df.dropna(inplace=True)
```
## Usage of Pandas Drop Rows with NaN Values in DataFrame
In real-world datasets, missing values are quite common. To remove all rows containing any NaN values in a Pandas DataFrame, you can simply use the `dropna()` method. This provides an easy way to clean your data by filtering out incomplete rows.
To run some examples of drop rows with NaN values in Pandas DataFrame, letâs create a Pandas DataFrame using data from a dictionary.
```
import pandas as pd
import numpy as np
technologies = ({
'Courses':["Spark",'Java',"Hadoop",'Python',np.nan],
'Fee' :[20000,np.nan,26000,24000,np.nan],
'Duration':['30days',np.nan,'35days','40days',np.nan],
'Discount':[1000,np.nan,2500,None,np.nan]
})
df = pd.DataFrame(technologies)
print("Create DataFrame:\n", df)
```
Yields below output.

## Drop Rows with NaN Values
You can use the `dropna()` method to remove rows with NaN (Not a Number) and None values from Pandas DataFrame. By default, it removes any row containing at least one NaN value and returns the copy of the DataFrame after removing rows. If you want to remove from the existing DataFrame, you should use `inplace=True`.
With NaN values in a Pandas DataFrame.
```
# Drop all rows that have NaN/None values
df2 = df.dropna()
print("After dropping the rows with NaN Values:\n", df2)
```
Yields below output.

`Related:` you can use the `dropna(axis=1)` to [drop all columns with NaN values](https://sparkbyexamples.com/pandas/pandas-drop-columns-with-nan-none-values/) from DataFrame.
Post-dropping rows with NaN, sometimes you may be required to reset the index, you can do so using the [DataFrame.reset\_index()](https://sparkbyexamples.com/pandas/pandas-reset-index-examples/) method.
```
# Reset index after drop
df2 = df.dropna().reset_index(drop=True)
print("Reset the index after dropping:\n", df2)
```
Yields below output.
```
# Output:
# Reset the index after dropping
Courses Fee Duration Discount
0 Spark 20000.0 30days 1000.0
1 Hadoop 26000.0 35days 2500.0
```
## Drop NaNs for all Columns in the DataFrame
Similarly, you can use `how` parameter of the `dropna()` function to specify which rows to drop based on NaN values. By default, the Param `how=any` specifies all rows with NaN/None values on any element are removed.
You can use `how='all'` to remove rows that have all NaN/None values in a row(data is missing for all elements in a row).
```
# Drop rows that has all NaN values
df2 = df.dropna(how='all')
print(" After dropping the rows which have all NaN values:\n", df2)
```
Yields below output.
```
# Output:
# After dropping the rows which have all NaN values:
Courses Fee Duration Discount
0 Spark 20000.0 30days 1000.0
1 Java NaN NaN NaN
2 Hadoop 26000.0 35days 2500.0
3 Python 24000.0 40days NaN
```
## Drop NaN Values on Selected Columns from List
Sometimes you may be required to drop rows only when selected columns have NaN/None values in DataFrame, you can achieve this by using `subset` param. This parameter takes a list of label names.
```
# Drop rows that has NaN values on selected columns
df2=df.dropna(subset=['Courses','Fee'])
print("After dropping rows based on specified columns:\n", df2)
```
Yields below output.
```
# Output:
# After dropping rows based on specified columns:
Courses Fee Duration Discount
0 Spark 20000.0 30days 1000.0
2 Hadoop 26000.0 35days 2500.0
3 Python 24000.0 40days NaN
```
## Drop Rows with NaN Values inplace
As you can see, by default `dropna()` method doesnât drop rows from the original DataFrame; instead, it returns a copy of the DataFrame. If you intend to modify the existing DataFrame directly, you can set `inplace=True`.
```
# Drop Rows with NaN Values inplace
df.dropna(inplace=True)
print("After dropping the rows with NaN values:\n", df)
# Output:
# After dropping the rows with NaN values:
# Courses Fee Duration Discount
# 0 Spark 20000.0 30days 1000.0
# 2 Hadoop 26000.0 35days 2500.0
```
## Complete Example of Drop Rows with NaN Values
```
import pandas as pd
import numpy as np
technologies = ({
'Courses':["Spark",'Java',"Hadoop",'Python',np.nan],
'Fee' :[20000,np.nan,26000,24000,np.nan],
'Duration':['30days',np.nan,'35days','40days',np.nan],
'Discount':[1000,np.nan,2500,None,np.nan]
})
df = pd.DataFrame(technologies)
print(df)
# Drop all rows with NaN values
df2=df.dropna()
print(df2)
df2=df.dropna(axis=0)
# Reset index after drop
df2=df.dropna().reset_index(drop=True)
print(df2)
# Drop row that has all NaN values
df2=df.dropna(how='all')
print(df2)
# Drop rows that has null on selected columns
df2=df.dropna(subset=['Courses','Fee'])
print(df2)
# Drop Rows with NaN Values inplace
df.dropna(inplace=True)
print(df)
```
## FAQ on Drop Rows with NaN Values
**How do I drop rows with NaN values in a Pandas DataFrame?**
You can use the `dropna()` method to remove rows with NaN values in a Pandas DataFrame. By default, it removes any of the rows having at least one NaN value. For instance, `df.dropna()`.
**What is the syntax for using the dropna() function to remove rows with NaN values?**
`df.dropna()` which returns a new DataFrame with rows that donât have NaN values.
**How can I drop rows with NaN values in a specific column?**
You can use the `subset` parameter of the `dropna()` method to specify a subset of columns to consider for NaN removal. For example, `df.dropna(subset=['specified_column'])`
**How can I drop rows based on multiple columns with NaN values?**
You can specify multiple columns in the `subset` parameter. For example, `df.dropna(subset=['specified_column1', 'specified_column2'])`
**How can I drop rows if all values in a row are NaN?**
You can use the `how` parameter with the value âallâ to drop rows where all values are NaN. For example, `df.dropna(how='all')`
## Conclusion
In this article, I have explained dropping rows with NaN/None values in pandas DataFrame using `DataFrame.dropna()`. Also learned to remove rows only when all values are NaN/None, remove only when selected columns have NaN values, and remove using the `inplace` parameter.
Happy Learning !\!
## Related Articles
- [Pandas Drop Rows by Index](https://sparkbyexamples.com/pandas/pandas-drop-rows-by-index/)
- [Pandas â Drop the First Three Rows](https://sparkbyexamples.com/pandas/pandas-drop-first-three-rows-of-a-dataframe)
- [Drop Pandas rows based on condition](https://sparkbyexamples.com/pandas/select-pandas-columns-based-on-condition/)
- [Delete Last Row From Pandas DataFrame](https://sparkbyexamples.com/pandas/pandas-delete-last-row-of-dataframe/)
- [Pandas â Drop List of Rows From DataFrame](https://sparkbyexamples.com/pandas/pandas-drop-list-of-rows-from-dataframe/)
- [Pandas Drop Last N Rows From DataFrame](https://sparkbyexamples.com/pandas/pandas-drop-last-n-rows-from-dataframe)
- [How to drop duplicate rows from DataFrame?](https://sparkbyexamples.com/pandas/pandas-drop-duplicate-rows-from-dataframe/)
- [Pandas Drop Rows Based on Column Value](https://sparkbyexamples.com/pandas/pandas-delete-rows-based-on-column-value/)
## References
- <https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.dropna.html?>
Tags: [DataFrame.dropna()](https://sparkbyexamples.com/tag/dataframe-dropna/), [DataFrame.reset\_index()](https://sparkbyexamples.com/tag/dataframe-reset-index/), [missing data](https://sparkbyexamples.com/tag/missing-data/)
#### LOGIN for Tutorial Menu
- [Log In](https://sparkbyexamples.com/login/)
## Top Tutorials
- [Apache Spark Tutorial](https://sparkbyexamples.com/)
- [PySpark Tutorial](https://sparkbyexamples.com/pyspark-tutorial/)
- [Python Pandas Tutorial](https://sparkbyexamples.com/python-pandas-tutorial-for-beginners/)
- [R Programming Tutorial](https://sparkbyexamples.com/r-tutorial-with-examples/)
- [Python NumPy Tutorial](https://sparkbyexamples.com/python-numpy-tutorial-for-beginners/)
- [Apache Hive Tutorial](https://sparkbyexamples.com/apache-hive-tutorial/)
- [Apache HBase Tutorial](https://sparkbyexamples.com/apache-hbase-tutorial/)
- [Apache Cassandra Tutorial](https://sparkbyexamples.com/apache-cassandra-tutorials-with-examples/)
- [Apache Kafka Tutorial](https://sparkbyexamples.com/apache-kafka-tutorials-with-examples/)
- [Snowflake Data Warehouse Tutorial](https://sparkbyexamples.com/snowflake-data-warehouse-database-tutorials/)
- [H2O Sparkling Water Tutorial](https://sparkbyexamples.com/h2o-sparkling-water-tutorial-beginners/)
## Categories
- [Apache Spark](https://sparkbyexamples.com/category/spark/)
- [PySpark](https://sparkbyexamples.com/category/pyspark/)
- [Pandas](https://sparkbyexamples.com/category/pandas/)
- [R Programming](https://sparkbyexamples.com/category/r-programming/)
- [Snowflake Database](https://sparkbyexamples.com/category/snowflake/)
- [NumPy](https://sparkbyexamples.com/category/numpy/)
- [Apache Hive](https://sparkbyexamples.com/category/apache-hive/)
- [Apache HBase](https://sparkbyexamples.com/category/hbase/)
- [Apache Kafka](https://sparkbyexamples.com/category/kafka/)
- [Apache Cassandra](https://sparkbyexamples.com/category/cassandra/)
- [H2O Sparkling Water](https://sparkbyexamples.com/category/h2o-sparkling-water/)
## Legal
- [SparkByExamples.com â Privacy Policy](https://sparkbyexamples.com/privacy-policy/)
- [Refund Policy](https://sparkbyexamples.com/refund-policy/)
- [Terms of Use](https://sparkbyexamples.com/terms-of-use/)

- Opens in a new tab
- Opens in a new tab
- Opens in a new tab
- Opens in a new tab
- Opens in a new tab
Copyright 2024 www.SparkByExamples.com. All rights reserved. |
| Readable Markdown | To drop rows with NaN (null) values in a Pandas DataFrame, you can use the `dropna()` function. Python doesnât support Null hence any missing data is represented as None or NaN. NaN stands for Not A Number and serves as one of the typical methods for indicating [missing values](https://sparkbyexamples.com/pandas/pandas-handle-missing-data-in-dataframe/) within datasets. None/NaN values are one of the major problems in Data Analysis hence before we process either you need to drop rows that have NaN values or [replace NaN with empty for Strings](https://sparkbyexamples.com/pandas/pandas-replace-nan-with-blank-empty-string/) and [replace NaN with zero for numeric columns](https://sparkbyexamples.com/pandas/pandas-replace-nan-values-by-zeroes-in-a-column/).
Advertisements
****Key Points â****
- Use the [dropna()](https://sparkbyexamples.com/pandas/pandas-dropna-usage-examples/) function in Pandas to remove rows containing `NaN/None` values from a DataFrame.
- `numpy.nan` is [Not a Number (NaN)](https://numpy.org/devdocs/reference/constants.html), which is of Python build-in numeric type float (floating point).
- `None` is of [NoneType](https://docs.python.org/2/library/types.html) and it is an object in Python.
- Specify the axis parameter as 0 to drop rows with NaN values.
- The `dropna()` function returns a new DataFrame with NaN-containing rows removed.
- Use additional parameters like `subset` to specify columns to consider for NaN removal, and `how` to control the criteria for dropping rows.
## Quick Examples of Dropping Rows with NaN Values
Below are quick examples of dropping rows with nan values.
```
# Quick examples of drop rows with nan values
# Example 1: Drop all rows with NaN values
df2=df.dropna()
df2=df.dropna(axis=0)
# Example 2: Reset index after drop
df2=df.dropna().reset_index(drop=True)
# Example 3: Drop row that has all NaN values
df2=df.dropna(how='all')
# Example 4: Drop rows that has NaN values on selected columns
df2=df.dropna(subset=['Courses','Fee'])
# Example 5: With threshold,
# Keep only the rows with at least 2 non-NA values.
df2=df.dropna(thresh=2)
# Example 6: Drop Rows with NaN Values inplace
df.dropna(inplace=True)
```
In real-world datasets, missing values are quite common. To remove all rows containing any NaN values in a Pandas DataFrame, you can simply use the `dropna()` method. This provides an easy way to clean your data by filtering out incomplete rows.
To run some examples of drop rows with NaN values in Pandas DataFrame, letâs create a Pandas DataFrame using data from a dictionary.
```
import pandas as pd
import numpy as np
technologies = ({
'Courses':["Spark",'Java',"Hadoop",'Python',np.nan],
'Fee' :[20000,np.nan,26000,24000,np.nan],
'Duration':['30days',np.nan,'35days','40days',np.nan],
'Discount':[1000,np.nan,2500,None,np.nan]
})
df = pd.DataFrame(technologies)
print("Create DataFrame:\n", df)
```
Yields below output.

## Drop Rows with NaN Values
You can use the `dropna()` method to remove rows with NaN (Not a Number) and None values from Pandas DataFrame. By default, it removes any row containing at least one NaN value and returns the copy of the DataFrame after removing rows. If you want to remove from the existing DataFrame, you should use `inplace=True`.
With NaN values in a Pandas DataFrame.
```
# Drop all rows that have NaN/None values
df2 = df.dropna()
print("After dropping the rows with NaN Values:\n", df2)
```
Yields below output.

`Related:` you can use the `dropna(axis=1)` to [drop all columns with NaN values](https://sparkbyexamples.com/pandas/pandas-drop-columns-with-nan-none-values/) from DataFrame.
Post-dropping rows with NaN, sometimes you may be required to reset the index, you can do so using the [DataFrame.reset\_index()](https://sparkbyexamples.com/pandas/pandas-reset-index-examples/) method.
```
# Reset index after drop
df2 = df.dropna().reset_index(drop=True)
print("Reset the index after dropping:\n", df2)
```
Yields below output.
```
# Output:
# Reset the index after dropping
Courses Fee Duration Discount
0 Spark 20000.0 30days 1000.0
1 Hadoop 26000.0 35days 2500.0
```
## Drop NaNs for all Columns in the DataFrame
Similarly, you can use `how` parameter of the `dropna()` function to specify which rows to drop based on NaN values. By default, the Param `how=any` specifies all rows with NaN/None values on any element are removed.
You can use `how='all'` to remove rows that have all NaN/None values in a row(data is missing for all elements in a row).
```
# Drop rows that has all NaN values
df2 = df.dropna(how='all')
print(" After dropping the rows which have all NaN values:\n", df2)
```
Yields below output.
```
# Output:
# After dropping the rows which have all NaN values:
Courses Fee Duration Discount
0 Spark 20000.0 30days 1000.0
1 Java NaN NaN NaN
2 Hadoop 26000.0 35days 2500.0
3 Python 24000.0 40days NaN
```
## Drop NaN Values on Selected Columns from List
Sometimes you may be required to drop rows only when selected columns have NaN/None values in DataFrame, you can achieve this by using `subset` param. This parameter takes a list of label names.
```
# Drop rows that has NaN values on selected columns
df2=df.dropna(subset=['Courses','Fee'])
print("After dropping rows based on specified columns:\n", df2)
```
Yields below output.
```
# Output:
# After dropping rows based on specified columns:
Courses Fee Duration Discount
0 Spark 20000.0 30days 1000.0
2 Hadoop 26000.0 35days 2500.0
3 Python 24000.0 40days NaN
```
## Drop Rows with NaN Values inplace
As you can see, by default `dropna()` method doesnât drop rows from the original DataFrame; instead, it returns a copy of the DataFrame. If you intend to modify the existing DataFrame directly, you can set `inplace=True`.
```
# Drop Rows with NaN Values inplace
df.dropna(inplace=True)
print("After dropping the rows with NaN values:\n", df)
# Output:
# After dropping the rows with NaN values:
# Courses Fee Duration Discount
# 0 Spark 20000.0 30days 1000.0
# 2 Hadoop 26000.0 35days 2500.0
```
## Complete Example of Drop Rows with NaN Values
```
import pandas as pd
import numpy as np
technologies = ({
'Courses':["Spark",'Java',"Hadoop",'Python',np.nan],
'Fee' :[20000,np.nan,26000,24000,np.nan],
'Duration':['30days',np.nan,'35days','40days',np.nan],
'Discount':[1000,np.nan,2500,None,np.nan]
})
df = pd.DataFrame(technologies)
print(df)
# Drop all rows with NaN values
df2=df.dropna()
print(df2)
df2=df.dropna(axis=0)
# Reset index after drop
df2=df.dropna().reset_index(drop=True)
print(df2)
# Drop row that has all NaN values
df2=df.dropna(how='all')
print(df2)
# Drop rows that has null on selected columns
df2=df.dropna(subset=['Courses','Fee'])
print(df2)
# Drop Rows with NaN Values inplace
df.dropna(inplace=True)
print(df)
```
## FAQ on Drop Rows with NaN Values
**How do I drop rows with NaN values in a Pandas DataFrame?**
You can use the `dropna()` method to remove rows with NaN values in a Pandas DataFrame. By default, it removes any of the rows having at least one NaN value. For instance, `df.dropna()`.
**What is the syntax for using the dropna() function to remove rows with NaN values?**
`df.dropna()` which returns a new DataFrame with rows that donât have NaN values.
**How can I drop rows with NaN values in a specific column?**
You can use the `subset` parameter of the `dropna()` method to specify a subset of columns to consider for NaN removal. For example, `df.dropna(subset=['specified_column'])`
**How can I drop rows based on multiple columns with NaN values?**
You can specify multiple columns in the `subset` parameter. For example, `df.dropna(subset=['specified_column1', 'specified_column2'])`
**How can I drop rows if all values in a row are NaN?**
You can use the `how` parameter with the value âallâ to drop rows where all values are NaN. For example, `df.dropna(how='all')`
## Conclusion
In this article, I have explained dropping rows with NaN/None values in pandas DataFrame using `DataFrame.dropna()`. Also learned to remove rows only when all values are NaN/None, remove only when selected columns have NaN values, and remove using the `inplace` parameter.
Happy Learning !\!
## Related Articles
- [Pandas Drop Rows by Index](https://sparkbyexamples.com/pandas/pandas-drop-rows-by-index/)
- [Pandas â Drop the First Three Rows](https://sparkbyexamples.com/pandas/pandas-drop-first-three-rows-of-a-dataframe)
- [Drop Pandas rows based on condition](https://sparkbyexamples.com/pandas/select-pandas-columns-based-on-condition/)
- [Delete Last Row From Pandas DataFrame](https://sparkbyexamples.com/pandas/pandas-delete-last-row-of-dataframe/)
- [Pandas â Drop List of Rows From DataFrame](https://sparkbyexamples.com/pandas/pandas-drop-list-of-rows-from-dataframe/)
- [Pandas Drop Last N Rows From DataFrame](https://sparkbyexamples.com/pandas/pandas-drop-last-n-rows-from-dataframe)
- [How to drop duplicate rows from DataFrame?](https://sparkbyexamples.com/pandas/pandas-drop-duplicate-rows-from-dataframe/)
- [Pandas Drop Rows Based on Column Value](https://sparkbyexamples.com/pandas/pandas-delete-rows-based-on-column-value/)
## References
- <https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.dropna.html?> |
| Shard | 46 (laksa) |
| Root Hash | 13197168827745396246 |
| Unparsed URL | com,sparkbyexamples!/pandas/pandas-drop-rows-with-nan-values-in-dataframe/ s443 |