ℹ️ 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.1 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://www.ionos.com/digitalguide/websites/web-development/python-pandas-dataframe-dropna/ |
| Last Crawled | 2026-04-05 00:15:11 (2 days ago) |
| First Indexed | 2025-07-02 11:54:55 (9 months ago) |
| HTTP Status Code | 200 |
| Meta Title | What is pandas dropna()? - IONOS |
| Meta Description | With the pandas dropna() function, you can clean datasets according to your needs. We show you how it works here. |
| Meta Canonical | null |
| Boilerpipe Text | The
Python pandas
DataFrame.dropna()
function is used to remove all rows or columns containing missing values (NaN) from a DataFrame. This makes it especially crucial for
preparing and cleaning data
.
What is the syntax for pandas
dropna()
?
The
dropna()
function accepts
up to five parameters
. Here’s its syntax:
DataFrame.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False, ignore_index=False)
python
Important parameters for
dropna()
You can
use parameters to influence the behavior
of the pandas
DataFrame.dropna()
function. Here’s an overview of the most important ones:
Parameter
Description
Default Value
axis
Determines whether rows (0 or
index
) or columns (1 or
columns
) will be removed
0
how
Specifies whether all (
all
) or only some (
any
) values must be NaN
any
thresh
Specifies the minimum number of non-NaN values a row or column must have to avoid being removed; cannot be combined with
how
optional
subset
Specifies which rows or columns should be considered
optional
inplace
Determines whether the operation is performed on the original DataFrame
False
ignore_index
If
True
, the remaining axis is labeled from 0 to n-1
False
How to use pandas
DataFrame.dropna()
Pandas
dropna()
is used to clean data before it’s analyzed. The removal of rows or columns with missing values helps to
prevent biases
in statistical evaluations. Since missing values can also lead to problems with data visualization, using the function is also
advantageous when creating charts
and reports.
Removing rows with missing values
In the following example, we’ll take a look at a DataFrame containing NaN values:
import pandas as pd
import numpy as np
# Creating a DataFrame with sample data
data = {
'A': [1, 2, np.nan, 4],
'B': [5, np.nan, np.nan, 8],
'C': [9, 10, 11, 12]
}
df = pd.DataFrame(data)
print(df)
python
The DataFrame looks like this:
A B C
0 1.0 5.0 9
1 2.0 NaN 10
2 NaN NaN 11
3 4.0 8.0 12
Next, we’re going to apply the pandas
dropna()
function:
## Remove all rows that contain at least one NaN value
df_cleaned = df.dropna()
print(df_cleaned)
python
Running the code above produces the following result:
A B C
0 1.0 5.0 9
3 4.0 8.0 12
Since all the other rows contain NaN values, only the zeroth and third rows remain.
Removing columns with missing values
Similarly, you can remove columns with missing values by setting the
axis
parameter to 1:
## Remove all columns that contain at least one NaN value
df_cleaned_columns = df.dropna(axis=1)
print(df_cleaned_columns)
python
Column C is the only column that remains, since it’s the only one that doesn’t contain NaN values:
C
0 9
1 10
2 11
3 12
Using
thresh
If you want to remove rows that contain fewer than two non-NaN values, you can use the
thresh
parameter:
## Only keeps rows that have 2 or more non-NaN values
df_thresh = df.dropna(thresh=2)
print(df_thresh)
python
Running the code produces the following output:
A B C
0 1.0 5.0 9
1 2.0 NaN 10
3 4.0 8.0 12
Row 1 is not removed from the output because it contains 2 non-NaN values (2.0 and 10).
Using
subset
The
subset
parameter allows you to specify the columns where the program should look for missing values. Only rows that contain missing values in the columns that have been specified will be removed.
## Removes all rows where column A contains a NaN value
df_subset = df.dropna(subset=['A'])
print(df_subset)
python
Here, only the second row is removed. The NaN value in the first row is ignored due to the subset parameter, which only takes column A into consideration:
A B C
0 1.0 5.0 9
1 2.0 NaN 10
3 4.0 8.0 12 |
| Markdown | [Skip to Main Content](https://www.ionos.com/digitalguide/websites/web-development/python-pandas-dataframe-dropna/#main-content)
[Digital Guide](https://www.ionos.com/digitalguide/)
IONOS Products
IONOS Products
Close
- Domains & SSL
- [Domain Names](https://www.ionos.com/domains/domain-names)
- [Free Domain Transfer](https://www.ionos.com/domains/domain-transfer)
- [.com Domain](https://www.ionos.com/domains/com-domain)
- [Free Domain](https://www.ionos.com/domains/free-domain-name)
- [New Top-Level Domains](https://www.ionos.com/domains/new-top-level-domains)
- [SSL Certificate](https://www.ionos.com/security/ssl-certificate)
- [Domain Security](https://www.ionos.com/domains/domain-guard)
- Websites & eCommerce
- [Website Builder](https://www.ionos.com/websites/website-builder)
- [Web Design Services](https://www.ionos.com/websites/custom-made-website)
- [Website Migration Service](https://www.ionos.com/websites/website-migration-service)
- [Search Engine Optimization Tool](https://www.ionos.com/marketing/rankingcoach)
- [Google Ads Management Service](https://www.ionos.com/marketing/google-ads-management)
- [AI Reputation Management](https://www.ionos.com/marketing/online-reputation-management)
- [eCommerce Website Builder](https://www.ionos.com/ecommerce-solutions/ecommerce-website-builder)
- [eCommerce Website Design](https://www.ionos.com/ecommerce-solutions/ecommerce-website-design)
- [eCommerce Plugin](https://www.ionos.com/ecommerce-solutions/ecommerce-plugin)
- [Social Buy Button](https://www.ionos.com/ecommerce-solutions/social-buy-button)
- Hosting & WordPress
- [Web Hosting](https://www.ionos.com/hosting/web-hosting)
- [Hosting for WordPress](https://www.ionos.com/hosting/managed-wordpress)
- [Hosting for WooCommerce](https://www.ionos.com/ecommerce-solutions/woocommerce-hosting)
- [Jamstack Hosting](https://www.ionos.com/hosting/jamstack)
- [Joomla! Hosting](https://www.ionos.com/hosting/joomla-hosting)
- [Deploy Now](https://www.ionos.com/hosting/deploy-now)
- [VPS Hosting](https://www.ionos.com/servers/vps)
- Office
- [Business Email](https://www.ionos.com/office-solutions/create-an-email-address)
- [Email Marketing Software](https://www.ionos.com/marketing/email-marketing)
- [Email Archiving Solutions](https://www.ionos.com/office-solutions/email-archiving)
- [Hosted Microsoft Exchange](https://www.ionos.com/office-solutions/microsoft-exchange)
- [Microsoft 365 Business](https://www.ionos.com/office-solutions/microsoft-office)
- [Google Workspace](https://www.ionos.com/office-solutions/google-workspace)
- [HiDrive Cloud Storage](https://www.ionos.com/office-solutions/hidrive-cloud-storage)
- [MyDefender](https://www.ionos.com/office-solutions/mydefender)
- Servers
- [Bare Metal Server](https://www.ionos.com/servers/bare-metal-servers)
- [Cloud Backup](https://www.ionos.com/cloud/cloud-backup)
- [Cloud Server](https://www.ionos.com/cloud/cloud-servers)
- [Dedicated Servers](https://www.ionos.com/servers/dedicated-servers)
- [Free VPS](https://www.ionos.com/servers/free-vps)
- [Managed Kubernetes](https://cloud.ionos.com/managed/kubernetes)
- [Rent a Server](https://www.ionos.com/servers/rent-a-server)
- [Windows VPS](https://www.ionos.com/servers/windows-vps)
- [VPS Hosting](https://www.ionos.com/servers/vps)
- [IONOS Cloud](https://cloud.ionos.com/)
- [IONOS Cloud Object Storage](https://cloud.ionos.com/storage/object-storage)
- Tools
- [Business Name Generator](https://www.ionos.com/tools/business-name-generator)
- [AI Domain Name Generator](https://www.ionos.com/ai-domain-name-generator)
- [Logo Creator](https://www.ionos.com/tools/logo-maker)
- [Favicon Generator](https://www.ionos.com/tools/favicon-generator)
- [Whois Lookup](https://www.ionos.com/tools/whois-domain-lookup)
- [Website Checker](https://www.ionos.com/tools/website-checker)
- [SEO Check](https://www.ionos.com/tools/seo-check)
- [SSL Checker](https://www.ionos.com/tools/ssl-checker)
- [IP Address Check](https://www.ionos.com/tools/ip-address)
Main menu
Close
IONOS Products
- [Websites](https://www.ionos.com/digitalguide/websites/ "Websites")
- [Website creation](https://www.ionos.com/digitalguide/websites/website-creation/ "Website creation")
- [Web design](https://www.ionos.com/digitalguide/websites/web-design/ "Web design")
- [Web development](https://www.ionos.com/digitalguide/websites/web-development/ "Web development")
- [Digital law](https://www.ionos.com/digitalguide/websites/digital-law/ "Digital law")
- [Hosting](https://www.ionos.com/digitalguide/hosting/ "Hosting")
- [CMS](https://www.ionos.com/digitalguide/hosting/cms/ "CMS")
- [Blogs](https://www.ionos.com/digitalguide/hosting/blogs/ "Blogs")
- [Technical matters](https://www.ionos.com/digitalguide/hosting/technical-matters/ "Technical matters")
- [Server](https://www.ionos.com/digitalguide/server/ "Server")
- [Know-how](https://www.ionos.com/digitalguide/server/know-how/ "Know-how")
- [Configuration](https://www.ionos.com/digitalguide/server/configuration/ "Configuration")
- [Security](https://www.ionos.com/digitalguide/server/security/ "Security")
- [Tools](https://www.ionos.com/digitalguide/server/tools/ "Tools")
- [Domains](https://www.ionos.com/digitalguide/domains/ "Domains")
- [Domain extensions](https://www.ionos.com/digitalguide/domains/domain-extensions/ "Domain extensions")
- [Domain administration](https://www.ionos.com/digitalguide/domains/domain-administration/ "Domain administration")
- [Domain news](https://www.ionos.com/digitalguide/domains/domain-news/ "Domain news")
- [Domain tips](https://www.ionos.com/digitalguide/domains/domain-tips/ "Domain tips")
- [Online marketing](https://www.ionos.com/digitalguide/online-marketing/ "Online marketing")
- [Online sales](https://www.ionos.com/digitalguide/online-marketing/online-sales/ "Online sales")
- [Search engine marketing](https://www.ionos.com/digitalguide/online-marketing/search-engine-marketing/ "Search engine marketing")
- [Social media](https://www.ionos.com/digitalguide/online-marketing/social-media/ "Social media")
- [Web analytics](https://www.ionos.com/digitalguide/online-marketing/web-analytics/ "Web analytics")
- [E-mail](https://www.ionos.com/digitalguide/e-mail/ "E-mail")
- [E-mail marketing](https://www.ionos.com/digitalguide/e-mail/e-mail-marketing/ "E-mail marketing")
- [E-mail security](https://www.ionos.com/digitalguide/e-mail/e-mail-security/ "E-mail security")
- [Technical matters](https://www.ionos.com/digitalguide/e-mail/technical-matters/ "Technical matters")
- [Startup](https://www.ionos.com/digitalguide/startup/ "Startup")
- [Get Started](https://www.ionos.com/digitalguide/startup/get-started/ "Get Started")
- [Grow Your Business](https://www.ionos.com/digitalguide/startup/grow-your-business/ "Grow Your Business")
- [Productivity](https://www.ionos.com/digitalguide/startup/productivity/ "Productivity")


See plans
Close
1. [IONOS](https://www.ionos.com/ "IONOS")
2. [Digital Guide](https://www.ionos.com/digitalguide/ "Digital Guide")
3. [Websites](https://www.ionos.com/digitalguide/websites/ "Websites")
4. [Web development](https://www.ionos.com/digitalguide/websites/web-development/ "Web development")
5. Python pandas: DataFrame dropna()
# How to clean data in pandas with dropna()
- [IONOS editorial team](https://www.ionos.com/experts/about-us/ "IONOS editorial team")
06/26/2025
3 mins
- - [Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https://www.ionos.com/digitalguide/websites/web-development/python-pandas-dataframe-dropna/ "Share on Facebook")
- [Share on Twitter](https://twitter.com/intent/tweet?source=https://www.ionos.com/digitalguide/websites/web-development/python-pandas-dataframe-dropna/&text=Python%20pandas%3A%20DataFrame%20dropna%28%29&hashtags=DigitalGuide&url=https://www.ionos.com/digitalguide/websites/web-development/python-pandas-dataframe-dropna/ "Share on Twitter")
- [Share on LinkedIn](https://www.linkedin.com/shareArticle?mini=true&url=https://www.ionos.com/digitalguide/websites/web-development/python-pandas-dataframe-dropna/ "Share on LinkedIn")
- [Python Pandas](https://www.ionos.com/digitalguide/tags/python-pandas/)

BEST-BACKGROUNDSShutterstock
Contents
- [What is the syntax for pandas dropna()?](https://www.ionos.com/digitalguide/websites/web-development/python-pandas-dataframe-dropna/#content-what-is-the-syntax-for-pandas-dropna)
- [How to use pandas DataFrame.dropna()](https://www.ionos.com/digitalguide/websites/web-development/python-pandas-dataframe-dropna/#content-how-to-use-pandas-dataframedropna)
The [Python pandas](https://www.ionos.com/digitalguide/websites/web-development/python-pandas/) `DataFrame.dropna()` function is used to remove all rows or columns containing missing values (NaN) from a DataFrame. This makes it especially crucial for **preparing and cleaning data**.
Web Hosting
Hosting that scales with your ambitions
- Stay online with 99.99% uptime and robust security
- Add performance with a click as traffic grows
- Includes free domain, SSL, email, and 24/7 support
See plans
## What is the syntax for pandas `dropna()`?
The `dropna()` function accepts **up to five parameters**. Here’s its syntax:
```
DataFrame.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False, ignore_index=False)
```
python
### Important parameters for `dropna()`
You can **use parameters to influence the behavior** of the pandas `DataFrame.dropna()` function. Here’s an overview of the most important ones:
| **Parameter** | **Description** | **Default Value** |
|---|---|---|
| `axis` | Determines whether rows (0 or `index`) or columns (1 or `columns`) will be removed | 0 |
| `how` | Specifies whether all (`all`) or only some (`any`) values must be NaN | `any` |
| `thresh` | Specifies the minimum number of non-NaN values a row or column must have to avoid being removed; cannot be combined with `how` | optional |
| `subset` | Specifies which rows or columns should be considered | optional |
| `inplace` | Determines whether the operation is performed on the original DataFrame | `False` |
| `ignore_index` | If `True`, the remaining axis is labeled from 0 to n-1 | `False` |
## How to use pandas `DataFrame.dropna()`
Pandas `dropna()` is used to clean data before it’s analyzed. The removal of rows or columns with missing values helps to **prevent biases** in statistical evaluations. Since missing values can also lead to problems with data visualization, using the function is also **advantageous when creating charts** and reports.
### Removing rows with missing values
In the following example, we’ll take a look at a DataFrame containing NaN values:
```
import pandas as pd
import numpy as np
# Creating a DataFrame with sample data
data = {
'A': [1, 2, np.nan, 4],
'B': [5, np.nan, np.nan, 8],
'C': [9, 10, 11, 12]
}
df = pd.DataFrame(data)
print(df)
```
python
The DataFrame looks like this:
```
A B C
0 1.0 5.0 9
1 2.0 NaN 10
2 NaN NaN 11
3 4.0 8.0 12
```
Next, we’re going to apply the pandas `dropna()` function:
```
## Remove all rows that contain at least one NaN value
df_cleaned = df.dropna()
print(df_cleaned)
```
python
Running the code above produces the following result:
```
A B C
0 1.0 5.0 9
3 4.0 8.0 12
```
Since all the other rows contain NaN values, only the zeroth and third rows remain.
### Removing columns with missing values
Similarly, you can remove columns with missing values by setting the `axis` parameter to 1:
```
## Remove all columns that contain at least one NaN value
df_cleaned_columns = df.dropna(axis=1)
print(df_cleaned_columns)
```
python
Column C is the only column that remains, since it’s the only one that doesn’t contain NaN values:
```
C
0 9
1 10
2 11
3 12
```
### Using `thresh`
If you want to remove rows that contain fewer than two non-NaN values, you can use the `thresh` parameter:
```
## Only keeps rows that have 2 or more non-NaN values
df_thresh = df.dropna(thresh=2)
print(df_thresh)
```
python
Running the code produces the following output:
```
A B C
0 1.0 5.0 9
1 2.0 NaN 10
3 4.0 8.0 12
```
Row 1 is not removed from the output because it contains 2 non-NaN values (2.0 and 10).
### Using `subset`
The `subset` parameter allows you to specify the columns where the program should look for missing values. Only rows that contain missing values in the columns that have been specified will be removed.
```
## Removes all rows where column A contains a NaN value
df_subset = df.dropna(subset=['A'])
print(df_subset)
```
python
Here, only the second row is removed. The NaN value in the first row is ignored due to the subset parameter, which only takes column A into consideration:
```
A B C
0 1.0 5.0 9
1 2.0 NaN 10
3 4.0 8.0 12
```
Related tags
- [Python Pandas](https://www.ionos.com/digitalguide/tags/python-pandas/)
[Go to Main Menu](https://www.ionos.com/digitalguide/websites/web-development/python-pandas-dataframe-dropna/#navigation-main)
### Related Products

Deploy Now
See plans


### 10 Years Digital Guide: A Success Story
Experience the Evolution
### Turn calls into revenue: AI receptionist
Free webinar
Save my spot
#### Popular Articles
##### [Domain Trading: How to sell a domain name](https://www.ionos.com/digitalguide/domains/domain-news/get-rich-by-selling-domain-names/ "Domain Trading: How to sell a domain name")
Buying and selling domains can be lucrative – if you know how to go about it. We will…
Read more
##### [What Nextcloud alternatives are there? 5 options compared](https://www.ionos.com/digitalguide/server/tools/nextcloud-alternatives/ "What Nextcloud alternatives are there? 5 options compared")
Looking for Nextcloud alternatives but unsure how they measure up? We take a look at 5…
Read more
##### [What are the best online backup services? A comparison](https://www.ionos.com/digitalguide/server/tools/the-best-online-backup-services/ "What are the best online backup services? A comparison")
Secure your data reliably in the cloud! These 7 online backup services offer safe and…
Read more
##### [How to upgrade Debian 13 step by step](https://www.ionos.com/digitalguide/server/configuration/debian-13-upgrade/ "How to upgrade Debian 13 step by step")
This guide shows you how to perform a Debian 13 upgrade, prepare your system, and avoid…
Read more
##### [Alternatives for InDesign: 4 free tools](https://www.ionos.com/digitalguide/websites/web-design/free-alternatives-to-adobe-indesign/ "Alternatives for InDesign: 4 free tools")
Desktop publishing with Adobe too expensive? There are numerous alternatives for InDesign…
Read more
### Related articles
BEST-BACKGROUNDSShutterstock
### [How to loop through DataFrames with pandas iterrows()](https://www.ionos.com/digitalguide/websites/web-development/python-pandas-dataframe-iterrows/ "How to loop through DataFrames with pandas iterrows()")
Pandas DataFrame.iterrows() is a helpful function for looping through rows in a DataFrame, especially when you need to process data row by row. This is especially useful for calculations or conditional logic. In this article, we’ll cover the syntax of panda iterrows() and show…
- Python Pandas
Read more
Mr. Kosalshutterstock
### [How to index pandas DataFrames](https://www.ionos.com/digitalguide/websites/web-development/python-pandas-dataframe-indexing/ "How to index pandas DataFrames")
Pandas DataFrame indexing is a powerful tool for efficient and effective data handling. With various methods, you can target specific data and subsets of your DataFrame. In this article, we’ll explore what the pandas DataFrame index is, how to access column and row data using…
- Python Pandas
Read more
Mr. Kosalshutterstock
### [What is Python pandas any() and how does it work?](https://www.ionos.com/digitalguide/websites/web-development/python-pandas-dataframe-any/ "What is Python pandas any() and how does it work?")
In pandas, the DataFrame any() method is an efficient tool to quickly check if there is at least one true value along an axis of a DataFrame. This method is especially helpful for data analysis and validation. In this article, we’ll show you what the syntax for this function is,…
- Python Pandas
Read more
ESB Professionalshutterstock
### [How to use Pandas DataFrame to manipulate tables quickly in Python](https://www.ionos.com/digitalguide/websites/web-development/python-pandas-dataframe/ "How to use Pandas DataFrame to manipulate tables quickly in Python")
The Pandas module is one of the most powerful tools for data manipulation in Python. One of the central data structures in Pandas is the DataFrame. DataFrames can be used to manipulate two-dimensional, structured data efficiently. We explain the structure of the data structure as…
- Python Pandas
Read more
![Image: What is the Python pandas property iloc\[\]?](https://www.ionos.com/digitalguide/fileadmin/_processed_/9/1/csm_docker-images-erstellen_830c6ccaca.webp)UndreyShutterstock
### [What is the Python pandas property iloc\[\]?](https://www.ionos.com/digitalguide/websites/web-development/python-pandas-dataframe-iloc/ "What is the Python pandas property iloc[]?")
When working with DataFrames in Python pandas, not all rows or columns of a DataFrame are always relevant for data analysis. The pandas DataFrame property iloc\[\] is a useful tool for selecting rows or columns using their indices. In this article, we’ll take a look at the syntax…
- Python Pandas
Read more
REDPIXEL.PLShutterstock
### [How to calculate averages with pandas mean()](https://www.ionos.com/digitalguide/websites/web-development/python-pandas-dataframe-mean/ "How to calculate averages with pandas mean()")
The pandas \`DataFrame.mean()\` function calculates averages in a DataFrame. It can be used to find average values for rows or columns, and offers flexibility when it comes to handling NaN values. In this article, we’ll look at the syntax of the function, the parameters it takes…
- Python Pandas
Read more
Your digital partner
- [About IONOS](https://www.ionos.com/about "About IONOS")
- [Careers](https://ionos.applicantpool.com/jobs/ "Careers")
- [Newsroom](https://www.ionos.com/newsroom/ "Newsroom")
- [Help Center](https://www.ionos.com/help/ "Help Center")
- [Terms and Conditions](https://www.ionos.com/terms-gtc/general-terms-and-conditions/ "Terms and Conditions")
- [Privacy Policy](https://www.ionos.com/terms-gtc/privacy-policy/ "Privacy Policy")
© 2026 [IONOS Inc.](https://www.ionos.com/ "IONOS Inc.")
- [RSS](https://www.ionos.com/digitalguide/websites/web-development/feed.rss "RSS")
- [LinkedIn](https://ionos.ly/IONOS_IN "LinkedIn")
- [Facebook](https://ionos.ly/IONOS_FB_US "Facebook")
- [YouTube](https://ionos.ly/IONOS_YT_US "YouTube")
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. [More Information.](https://www.ionos.com/cookies)
OK |
| Readable Markdown | The [Python pandas](https://www.ionos.com/digitalguide/websites/web-development/python-pandas/) `DataFrame.dropna()` function is used to remove all rows or columns containing missing values (NaN) from a DataFrame. This makes it especially crucial for **preparing and cleaning data**.
## What is the syntax for pandas `dropna()`?
The `dropna()` function accepts **up to five parameters**. Here’s its syntax:
```
DataFrame.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False, ignore_index=False)
```
python
### Important parameters for `dropna()`
You can **use parameters to influence the behavior** of the pandas `DataFrame.dropna()` function. Here’s an overview of the most important ones:
| **Parameter** | **Description** | **Default Value** |
|---|---|---|
| `axis` | Determines whether rows (0 or `index`) or columns (1 or `columns`) will be removed | 0 |
| `how` | Specifies whether all (`all`) or only some (`any`) values must be NaN | `any` |
| `thresh` | Specifies the minimum number of non-NaN values a row or column must have to avoid being removed; cannot be combined with `how` | optional |
| `subset` | Specifies which rows or columns should be considered | optional |
| `inplace` | Determines whether the operation is performed on the original DataFrame | `False` |
| `ignore_index` | If `True`, the remaining axis is labeled from 0 to n-1 | `False` |
## How to use pandas `DataFrame.dropna()`
Pandas `dropna()` is used to clean data before it’s analyzed. The removal of rows or columns with missing values helps to **prevent biases** in statistical evaluations. Since missing values can also lead to problems with data visualization, using the function is also **advantageous when creating charts** and reports.
### Removing rows with missing values
In the following example, we’ll take a look at a DataFrame containing NaN values:
```
import pandas as pd
import numpy as np
# Creating a DataFrame with sample data
data = {
'A': [1, 2, np.nan, 4],
'B': [5, np.nan, np.nan, 8],
'C': [9, 10, 11, 12]
}
df = pd.DataFrame(data)
print(df)
```
python
The DataFrame looks like this:
```
A B C
0 1.0 5.0 9
1 2.0 NaN 10
2 NaN NaN 11
3 4.0 8.0 12
```
Next, we’re going to apply the pandas `dropna()` function:
```
## Remove all rows that contain at least one NaN value
df_cleaned = df.dropna()
print(df_cleaned)
```
python
Running the code above produces the following result:
```
A B C
0 1.0 5.0 9
3 4.0 8.0 12
```
Since all the other rows contain NaN values, only the zeroth and third rows remain.
### Removing columns with missing values
Similarly, you can remove columns with missing values by setting the `axis` parameter to 1:
```
## Remove all columns that contain at least one NaN value
df_cleaned_columns = df.dropna(axis=1)
print(df_cleaned_columns)
```
python
Column C is the only column that remains, since it’s the only one that doesn’t contain NaN values:
```
C
0 9
1 10
2 11
3 12
```
### Using `thresh`
If you want to remove rows that contain fewer than two non-NaN values, you can use the `thresh` parameter:
```
## Only keeps rows that have 2 or more non-NaN values
df_thresh = df.dropna(thresh=2)
print(df_thresh)
```
python
Running the code produces the following output:
```
A B C
0 1.0 5.0 9
1 2.0 NaN 10
3 4.0 8.0 12
```
Row 1 is not removed from the output because it contains 2 non-NaN values (2.0 and 10).
### Using `subset`
The `subset` parameter allows you to specify the columns where the program should look for missing values. Only rows that contain missing values in the columns that have been specified will be removed.
```
## Removes all rows where column A contains a NaN value
df_subset = df.dropna(subset=['A'])
print(df_subset)
```
python
Here, only the second row is removed. The NaN value in the first row is ignored due to the subset parameter, which only takes column A into consideration:
```
A B C
0 1.0 5.0 9
1 2.0 NaN 10
3 4.0 8.0 12
``` |
| Shard | 160 (laksa) |
| Root Hash | 16735937331567176760 |
| Unparsed URL | com,ionos!www,/digitalguide/websites/web-development/python-pandas-dataframe-dropna/ s443 |