🕷️ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 83 (from laksa109)

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
12 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://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe
Last Crawled2026-04-14 12:30:53 (12 hours ago)
First Indexed2022-08-04 04:38:25 (3 years ago)
HTTP Status Code200
Meta TitleHow To Use Python pandas dropna() to Drop NA Values from DataFrame | DigitalOcean
Meta DescriptionTechnical tutorials, Q&A, events — This is an inclusive place where developers can find or lend support and discover new ways to contribute to the community.
Meta Canonicalnull
Boilerpipe Text
Introduction In this tutorial, you’ll learn how to use panda’s DataFrame dropna() function. NA values are “Not Available”. This can apply to Null , None , pandas.NaT , or numpy.nan . Using dropna() will drop the rows and columns with these values. This can be beneficial to provide you with only valid data. By default, this function returns a new DataFrame and the source DataFrame remains unchanged. This tutorial was verified with Python 3.10.9, pandas 1.5.2, and NumPy 1.24.1. Syntax dropna() takes the following parameters: dropna ( self , axis = 0 , how = "any" , thresh = None , subset = None , inplace = False ) axis : {0 (or 'index'), 1 (or 'columns')}, default 0 If 0 , drop rows with missing values. If 1 , drop columns with missing values. how : {'any', 'all'}, default 'any' If 'any' , drop the row or column if any of the values is NA . If 'all' , drop the row or column if all of the values are NA . thresh : (optional) an int value to specify the threshold for the drop operation. subset : (optional) column label or sequence of labels to specify rows or columns. inplace : (optional) a bool value. If True , the source DataFrame is changed and None is returned. Constructing Sample DataFrames Construct a sample DataFrame that contains valid and invalid values: dropnaExample.py import pandas as pd import numpy as np d1 = { 'Name' : [ 'Shark' , 'Whale' , 'Jellyfish' , 'Starfish' ] , 'ID' : [ 1 , 2 , 3 , 4 ] , 'Population' : [ 100 , 200 , np . nan , pd . NaT ] , 'Regions' : [ 1 , None , pd . NaT , pd . NaT ] } df1 = pd . DataFrame ( d1 ) print ( df1 ) This code will print out the DataFrame: Output Name ID Population Regions 0 Shark 1 100 1 1 Whale 2 200 None 2 Jellyfish 3 NaN NaT 3 Starfish 4 NaT NaT Then add a second DataFrame with additional rows and columns with NA values: d2 = { 'Name' : [ 'Shark' , 'Whale' , 'Jellyfish' , 'Starfish' , pd . NaT ] , 'ID' : [ 1 , 2 , 3 , 4 , pd . NaT ] , 'Population' : [ 100 , 200 , np . nan , pd . NaT , pd . NaT ] , 'Regions' : [ 1 , None , pd . NaT , pd . NaT , pd . NaT ] , 'Endangered' : [ pd . NaT , pd . NaT , pd . NaT , pd . NaT , pd . NaT ] } df2 = pd . DataFrame ( d2 ) print ( df2 ) This will output a new DataFrame: Output Name ID Population Regions Endangered 0 Shark 1 100 1 NaT 1 Whale 2 200 None NaT 2 Jellyfish 3 NaN NaT NaT 3 Starfish 4 NaT NaT NaT 4 NaT NaT NaT NaT NaT You will use the preceding DataFrames in the examples that follow. Dropping All Rows with Missing Values Use dropna() to remove rows with any None , NaN , or NaT values: dropnaExample.py dfresult = df1 . dropna ( ) print ( dfresult ) This will output: Output Name ID Population Regions 0 Shark 1 100 1 A new DataFrame with a single row that didn’t contain any NA values. Dropping All Columns with Missing Values Use dropna() with axis=1 to remove columns with any None , NaN , or NaT values: dfresult = df1 . dropna ( axis = 1 ) print ( dfresult ) The columns with any None , NaN , or NaT values will be dropped: Output Name ID 0 Shark 1 1 Whale 2 2 Jellyfish 3 3 Starfish 4 A new DataFrame with a single column that contained non- NA values. Dropping Rows or Columns if all the Values are Null with how Use the second DataFrame and how : dropnaExample.py dfresult = df2 . dropna ( how = 'all' ) print ( dfresult ) The rows with all values equal to NA will be dropped: Output Name ID Population Regions Endangered 0 Shark 1 100 1 NaT 1 Whale 2 200 None NaT 2 Jellyfish 3 NaN NaT NaT 3 Starfish 4 NaT NaT NaT The fifth row was dropped. Next, use how and specify the axis : dropnaExample.py dfresult = df2 . dropna ( how = 'all' , axis = 1 ) print ( dfresult ) The columns with all values equal to NA will be dropped: Output Name ID Population Regions 0 Shark 1 100 1 1 Whale 2 200 None 2 Jellyfish 3 NaN NaT 3 Starfish 4 NaT NaT 4 NaT NaT NaT NaT The fifth column was dropped. Dropping Rows or Columns if a Threshold is Crossed with thresh Use the second DataFrame with thresh to drop rows that do not meet the threshold of at least 3 non- NA values: dropnaExample.py dfresult = df2 . dropna ( thresh = 3 ) print ( dfresult ) The rows do not have at least 3 non- NA will be dropped: Output Name ID Population Regions Endangered 0 Shark 1 100 1 NaT 1 Whale 2 200 None NaT The third, fourth, and fifth rows were dropped. Dropping Rows or Columns for Specific subsets Use the second DataFrame with subset to drop rows with NA values in the Population column: dropnaExample.py dfresult = df2 . dropna ( subset = [ 'Population' ] ) print ( dfresult ) The rows that have Population with NA values will be dropped: Output Name ID Population Regions Endangered 0 Shark 1 100 1 NaT 1 Whale 2 200 None NaT The third, fourth, and fifth rows were dropped. You can also specify the index values in the subset when dropping columns from the DataFrame: dropnaExample.py dfresult = df2 . dropna ( subset = [ 1 , 2 ] , axis = 1 ) print ( dfresult ) The columns that contain NA values in subset of rows 1 and 2 : Output Name ID 0 Shark 1 1 Whale 2 2 Jellyfish 3 3 Starfish 4 4 NaT NaT The third, fourth, and fifth columns were dropped. Changing the source DataFrame after Dropping Rows or Columns with inplace By default, dropna() does not modify the source DataFrame. However, in some cases, you may wish to save memory when working with a large source DataFrame by using inplace . dropnaExample.py df1 . dropna ( inplace = True ) print ( df1 ) This code does not use a dfresult variable. This will output: Output Name ID Population Regions 0 Shark 1 100 1 The original DataFrame has been modified. Conclusion In this article, you used the dropna() function to remove rows and columns with NA values. Continue your learning with more Python and pandas tutorials - Python pandas Module Tutorial , pandas Drop Duplicate Rows . References pandas DataFrame dropna() API Doc Was this helpful? This work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Markdown
[DigitalOcean acquires Katanemo Labs, Inc. to accelerate AI development](https://www.digitalocean.com/blog/digitalocean-acquires-katanemo-labs-inc) [Now Available: Cloud Security Posture Management (CSPM)](https://www.digitalocean.com/blog/now-available-cloud-security-posture-management) [Now Available: OpenAI GPT-5.4 on the Gradient™ AI Platform](https://www.digitalocean.com/blog/whats-new-on-gradient-ai-platform#week-of-march-2) [Now Available: OpenAI GPT-5.3 Codex on the Gradient™ AI Platform](https://www.digitalocean.com/blog/whats-new-on-gradient-ai-platform#week-of-february-23rd) [Now Available: Claude Sonnet 4.6 on the Gradient™ AI Platform](https://www.digitalocean.com/blog/whats-new-on-gradient-ai-platform#week-of-february-17th) [Join us for Deploy in San Francisco, on April 28, 2026. Register now](https://www.digitalocean.com/deploy) - [Blog](https://www.digitalocean.com/blog) - [Docs](https://docs.digitalocean.com/products) - [Careers](https://www.digitalocean.com/careers) - [Get Support](https://www.digitalocean.com/support) - [Contact Sales](https://www.digitalocean.com/company/contact/sales?referrer=tophat) [DigitalOcean](https://www.digitalocean.com/) - Products Featured Products Gradient™ AI Agentic Inference Cloud Build, train, deploy apps/agents and scale at will Compute Build, deploy, and scale cloud compute resources Containers and Images Safely store and manage containers and backups Managed Databases Fully managed resources running popular database engines Management and Dev Tools Control infrastructure and gather insights Networking Secure and control traffic to apps Security Store and access any amount of data reliably in the cloud Storage Store and access any amount of data reliably in the cloud [Browse all products](https://www.digitalocean.com/products) ### Featured Products [Gradient™ AI Agentic Cloud Build, train, deploy apps/agents and scale at will](https://www.digitalocean.com/products/gradient) [Droplets Reliable, powerful, Linux-based virtual machines New updates](https://www.digitalocean.com/products/droplets) [Managed Databases Fully managed resources running popular database engines](https://www.digitalocean.com/products/managed-databases) [App Platform Publish code directly to DigitalOcean servers](https://www.digitalocean.com/products/app-platform) [Gradient™ AI GPU Droplets GPU-powered virtual machines to build and run AI apps](https://www.digitalocean.com/products/gradient/gpu-droplets) [Kubernetes Fully managed, highly available K8s with autoscale.](https://www.digitalocean.com/products/kubernetes) [Browse all products](https://www.digitalocean.com/products) - Solutions AI/ML CMS Data and IoT Developer Tools Gaming and Media GPU Hosting Security and Networking Startups and SMBs Web and App Platforms [See all solutions](https://www.digitalocean.com/solutions) ### AI/ML [AI HR Knowledge Assistant Deploy AI tools developed for Human Resources](https://www.digitalocean.com/solutions/ai-hr-knowledge-assistant) [AI Code Copilot Write, complete, and review code faster](https://www.digitalocean.com/solutions/ai-code-copilot) [AI Support Ticket Triage Auto-classify and route customer support tickets](https://www.digitalocean.com/solutions/ai-support-ticket-triage) [AI Recommendation Engine Build tools to personalize user experiences](https://www.digitalocean.com/solutions/ai-recommendation-engine) [AI Agent Builder Create, deploy, and manage AI agents for anything](https://www.digitalocean.com/solutions/ai-agent-builder) [Multimodal AI Run models that process multiple data types](https://www.digitalocean.com/solutions/multimodal-ai) [Low-Latency Inference Run AI models quickly and efficiently](https://www.digitalocean.com/solutions/low-latency-inference) Not seeing what you are looking for? [Browse all AI/ML solutions](https://www.digitalocean.com/solutions) [Need help? Talk to an expert](https://www.digitalocean.com/company/contact/sales?referrer=mainmenu/solutions) [Get migration assistance](https://www.digitalocean.com/migrate) - Developers Community Documentation Developer Tools Get Involved Utilities and Help ### Community [Blog The latest product releases and insights from our experts](https://www.digitalocean.com/blog) [CSS Tricks All things web design](https://css-tricks.com/) [The Wave Content to level up your business](https://www.digitalocean.com/resources) [Tutorials Our extensive library of technical articles](https://www.digitalocean.com/community/tutorials) [Q\&A Answers to your cloud and coding questions](https://www.digitalocean.com/community/questions) [YouTube Educational content and tutorials](https://youtube.com/DigitalOcean) [Visit DigitalOcean's Community Site](https://www.digitalocean.com/community) - Partners Become a Partner Marketplace ### Become a Partner [About Partner Pod Discounts, revenue share, and more for businesses](https://www.digitalocean.com/partners/pod) [AI Partner Program Boost your AI business](https://www.digitalocean.com/partners/ai-partner-program) [DigitalOcean Startups Resources and support to propel your startup](https://www.digitalocean.com/startups) [Partners Directory Current list of DigitalOcean Partners](https://www.digitalocean.com/partners/directory) [Customer Stories Customers growing on DigitalOcean](https://www.digitalocean.com/customers) [Need help? Talk to an expert](https://www.digitalocean.com/company/contact/sales?referrer=mainmenu/partners) [Become a Partner](https://www.digitalocean.com/partners/apply) - [Pricing](https://www.digitalocean.com/pricing) - Log in - [Community](https://www.digitalocean.com/api/dynamic-content/v1/login?success_redirect=https%3A%2F%2Fwww.digitalocean.com%2Fcommunity%2Ftutorials%2Fpandas-dropna-drop-null-na-values-from-dataframe&error_redirect=https%3A%2F%2Fwww.digitalocean.com%2Fauth-error&type=login) - [DigitalOcean](https://cloud.digitalocean.com/login) - Sign up - [Community](https://www.digitalocean.com/api/dynamic-content/v1/login?success_redirect=https%3A%2F%2Fwww.digitalocean.com%2Fcommunity%2Ftutorials%2Fpandas-dropna-drop-null-na-values-from-dataframe&error_redirect=https%3A%2F%2Fwww.digitalocean.com%2Fauth-error&type=register) - [DigitalOcean](https://cloud.digitalocean.com/registrations/new) - Log in - [Community](https://www.digitalocean.com/api/dynamic-content/v1/login?success_redirect=https%3A%2F%2Fwww.digitalocean.com%2Fcommunity%2Ftutorials%2Fpandas-dropna-drop-null-na-values-from-dataframe&error_redirect=https%3A%2F%2Fwww.digitalocean.com%2Fauth-error&type=login) - [DigitalOcean](https://cloud.digitalocean.com/login) - Sign up - [Community](https://www.digitalocean.com/api/dynamic-content/v1/login?success_redirect=https%3A%2F%2Fwww.digitalocean.com%2Fcommunity%2Ftutorials%2Fpandas-dropna-drop-null-na-values-from-dataframe&error_redirect=https%3A%2F%2Fwww.digitalocean.com%2Fauth-error&type=register) - [DigitalOcean](https://cloud.digitalocean.com/registrations/new) - [Tutorials](https://www.digitalocean.com/community/tutorials) - [Questions](https://www.digitalocean.com/community/questions) - [Product Docs](https://docs.digitalocean.com/) - Search Community ## Report this What is the reason for this report? This undefined is spam This undefined is offensive This undefined is off-topic This undefined is other Submit ## Table of contents 1. [Syntax](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#syntax) 2. [Constructing Sample DataFrames](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#constructing-sample-dataframes) 3. [Dropping All Rows with Missing Values](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#dropping-all-rows-with-missing-values) 4. [Dropping All Columns with Missing Values](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#dropping-all-columns-with-missing-values) 5. [Dropping Rows or Columns if all the Values are Null with how](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#dropping-rows-or-columns-if-all-the-values-are-null-with-how) 6. [Dropping Rows or Columns if a Threshold is Crossed with thresh](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#dropping-rows-or-columns-if-a-threshold-is-crossed-with-thresh) 7. [Dropping Rows or Columns for Specific subsets](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#dropping-rows-or-columns-for-specific-subsets) 8. [Changing the source DataFrame after Dropping Rows or Columns with inplace](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#changing-the-source-dataframe-after-dropping-rows-or-columns-with-inplace) 9. [Conclusion](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#conclusion) 1. [Tutorials](https://www.digitalocean.com/community/tutorials?subtype=tutorial) 2. [Pandas](https://www.digitalocean.com/community/tags/pandas) 3. How To Use Python pandas dropna() to Drop NA Values from DataFrame [Tutorial](https://www.digitalocean.com/community/tutorials?subtype=tutorial) # How To Use Python pandas dropna() to Drop NA Values from DataFrame Published on August 3, 2022 [Pandas](https://www.digitalocean.com/community/tags/pandas) [Python](https://www.digitalocean.com/community/tags/python) ![Pankaj Kumar](https://www.gravatar.com/avatar/dcb62410bd20fd354c21af393bec6e67b235a09758848745cfd4405e05bba1f4?default=retro&size=256)![Bradley Kouchi](https://www.gravatar.com/avatar/1c15a76c8f5e79c35d1ce6c479888827d77e10dac512f9476549d036ca44fc17?default=retro&size=256) By [Pankaj Kumar](https://www.digitalocean.com/community/users/pankajkumarjournaldev) and [Bradley Kouchi](https://www.digitalocean.com/community/users/bkouchido) ![How To Use Python pandas dropna() to Drop NA Values from DataFrame](https://www.digitalocean.com/api/static-content/v1/images?src=https%3A%2F%2Fjournaldev.nyc3.cdn.digitaloceanspaces.com%2F2019%2F11%2Fpandas-dropna.png&width=1920) Table of contents Popular topics ### [Introduction](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#introduction) In this tutorial, you’ll learn how to use panda’s DataFrame `dropna()` function. `NA` values are “Not Available”. This can apply to `Null`, `None`, `pandas.NaT`, or `numpy.nan`. Using `dropna()` will drop the rows and columns with these values. This can be beneficial to provide you with only valid data. By default, this function returns a new DataFrame and the source DataFrame remains unchanged. This tutorial was verified with Python 3.10.9, pandas 1.5.2, and NumPy 1.24.1. ## [Syntax](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#syntax) `dropna()` takes the following parameters: ``` dropna(self, axis=0, how="any", thresh=None, subset=None, inplace=False) ``` Copy - `axis`: `{0 (or 'index'), 1 (or 'columns')}, default 0` - If `0`, drop rows with missing values. - If `1`, drop columns with missing values. - `how`: `{'any', 'all'}, default 'any'` - If `'any'`, drop the row or column if any of the values is `NA`. - If `'all'`, drop the row or column if all of the values are `NA`. - `thresh`: (optional) an `int` value to specify the threshold for the drop operation. - `subset`: (optional) column label or sequence of labels to specify rows or columns. - `inplace`: (optional) a `bool` value. - If `True`, the source DataFrame is changed and `None` is returned. ## [Constructing Sample DataFrames](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#constructing-sample-dataframes) Construct a sample DataFrame that contains valid and invalid values: dropnaExample.py ``` import pandas as pd import numpy as np d1 = { 'Name': ['Shark', 'Whale', 'Jellyfish', 'Starfish'], 'ID': [1, 2, 3, 4], 'Population': [100, 200, np.nan, pd.NaT], 'Regions': [1, None, pd.NaT, pd.NaT] } df1 = pd.DataFrame(d1) print(df1) ``` Copy This code will print out the DataFrame: ``` OutputName ID Population Regions 0 Shark 1 100 1 1 Whale 2 200 None 2 Jellyfish 3 NaN NaT 3 Starfish 4 NaT NaT ``` Then add a second DataFrame with additional rows and columns with `NA` values: ``` d2 = { 'Name': ['Shark', 'Whale', 'Jellyfish', 'Starfish', pd.NaT], 'ID': [1, 2, 3, 4, pd.NaT], 'Population': [100, 200, np.nan, pd.NaT, pd.NaT], 'Regions': [1, None, pd.NaT, pd.NaT, pd.NaT], 'Endangered': [pd.NaT, pd.NaT, pd.NaT, pd.NaT, pd.NaT] } df2 = pd.DataFrame(d2) print(df2) ``` Copy This will output a new DataFrame: ``` OutputName ID Population Regions Endangered 0 Shark 1 100 1 NaT 1 Whale 2 200 None NaT 2 Jellyfish 3 NaN NaT NaT 3 Starfish 4 NaT NaT NaT 4 NaT NaT NaT NaT NaT ``` You will use the preceding DataFrames in the examples that follow. ## [Dropping All Rows with Missing Values](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#dropping-all-rows-with-missing-values) Use `dropna()` to remove rows with any `None`, `NaN`, or `NaT` values: dropnaExample.py ``` dfresult = df1.dropna() print(dfresult) ``` Copy This will output: ``` OutputName ID Population Regions 0 Shark 1 100 1 ``` A new DataFrame with a single row that didn’t contain any `NA` values. ## [Dropping All Columns with Missing Values](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#dropping-all-columns-with-missing-values) Use `dropna()` with `axis=1` to remove columns with any `None`, `NaN`, or `NaT` values: ``` dfresult = df1.dropna(axis=1) print(dfresult) ``` Copy The columns with any `None`, `NaN`, or `NaT` values will be dropped: ``` OutputName ID 0 Shark 1 1 Whale 2 2 Jellyfish 3 3 Starfish 4 ``` A new DataFrame with a single column that contained non-`NA` values. ## [Dropping Rows or Columns if `all` the Values are `Null` with `how`](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#dropping-rows-or-columns-if-all-the-values-are-null-with-how) Use the second DataFrame and `how`: dropnaExample.py ``` dfresult = df2.dropna(how='all') print(dfresult) ``` Copy The rows with `all` values equal to `NA` will be dropped: ``` OutputName ID Population Regions Endangered 0 Shark 1 100 1 NaT 1 Whale 2 200 None NaT 2 Jellyfish 3 NaN NaT NaT 3 Starfish 4 NaT NaT NaT ``` The fifth row was dropped. Next, use `how` and specify the `axis`: dropnaExample.py ``` dfresult = df2.dropna(how='all', axis=1) print(dfresult) ``` Copy The columns with `all` values equal to `NA` will be dropped: ``` OutputName ID Population Regions 0 Shark 1 100 1 1 Whale 2 200 None 2 Jellyfish 3 NaN NaT 3 Starfish 4 NaT NaT 4 NaT NaT NaT NaT ``` The fifth column was dropped. ## [Dropping Rows or Columns if a Threshold is Crossed with `thresh`](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#dropping-rows-or-columns-if-a-threshold-is-crossed-with-thresh) Use the second DataFrame with `thresh` to drop rows that do not meet the threshold of at least `3` non-`NA` values: dropnaExample.py ``` dfresult = df2.dropna(thresh=3) print(dfresult) ``` Copy The rows do not have at least `3` non-`NA` will be dropped: ``` OutputName ID Population Regions Endangered 0 Shark 1 100 1 NaT 1 Whale 2 200 None NaT ``` The third, fourth, and fifth rows were dropped. ## [Dropping Rows or Columns for Specific `subsets`](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#dropping-rows-or-columns-for-specific-subsets) Use the second DataFrame with `subset` to drop rows with `NA` values in the `Population` column: dropnaExample.py ``` dfresult = df2.dropna(subset=['Population']) print(dfresult) ``` Copy The rows that have `Population` with `NA` values will be dropped: ``` OutputName ID Population Regions Endangered 0 Shark 1 100 1 NaT 1 Whale 2 200 None NaT ``` The third, fourth, and fifth rows were dropped. You can also specify the `index` values in the `subset` when dropping columns from the DataFrame: dropnaExample.py ``` dfresult = df2.dropna(subset=[1, 2], axis=1) print(dfresult) ``` Copy The columns that contain `NA` values in subset of rows `1` and `2`: ``` OutputName ID 0 Shark 1 1 Whale 2 2 Jellyfish 3 3 Starfish 4 4 NaT NaT ``` The third, fourth, and fifth columns were dropped. ## [Changing the source DataFrame after Dropping Rows or Columns with `inplace`](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#changing-the-source-dataframe-after-dropping-rows-or-columns-with-inplace) By default, `dropna()` does not modify the source DataFrame. However, in some cases, you may wish to save memory when working with a large source DataFrame by using `inplace`. dropnaExample.py ``` df1.dropna(inplace=True) print(df1) ``` Copy This code does not use a `dfresult` variable. This will output: ``` OutputName ID Population Regions 0 Shark 1 100 1 ``` The original DataFrame has been modified. ## [Conclusion](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#conclusion) In this article, you used the `dropna()` function to remove rows and columns with `NA` values. Continue your learning with more Python and pandas tutorials - [Python pandas Module Tutorial](https://www.digitalocean.com/community/tutorials/python-pandas-module-tutorial), [pandas Drop Duplicate Rows](https://www.digitalocean.com/community/tutorials/pandas-drop-duplicate-rows-drop_duplicates-function). **References** - [pandas DataFrame `dropna()` API Doc](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.dropna.html) Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases. [Learn more about our products](https://www.digitalocean.com/products "Learn more about our products") ### About the author(s) ![Pankaj Kumar](https://www.gravatar.com/avatar/dcb62410bd20fd354c21af393bec6e67b235a09758848745cfd4405e05bba1f4?default=retro&size=256) Pankaj Kumar Author [See author profile](https://www.digitalocean.com/community/users/pankajkumarjournaldev) Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev [See author profile](https://www.digitalocean.com/community/users/pankajkumarjournaldev) ![Bradley Kouchi](https://www.gravatar.com/avatar/1c15a76c8f5e79c35d1ce6c479888827d77e10dac512f9476549d036ca44fc17?default=retro&size=256) Bradley Kouchi Editor [See author profile](https://www.digitalocean.com/community/users/bkouchido) [See author profile](https://www.digitalocean.com/community/users/bkouchido) Category: [Tutorial](https://www.digitalocean.com/community/tutorials?subtype=tutorial) Tags: [Pandas](https://www.digitalocean.com/community/tags/pandas) [Python](https://www.digitalocean.com/community/tags/python) #### Still looking for an answer? [Ask a question](https://www.digitalocean.com/community/questions)[Search for more help](https://www.digitalocean.com/community) Was this helpful? Yes No Comments(2) Follow-up questions(0) ![JournalDev](https://www.gravatar.com/avatar/7c7a4546c8430a5a91a338a177bf8e7e8e153fe7eaee076acfa6d46029efef49?default=retro) [JournalDev](https://www.digitalocean.com/community/users/journaldev)![DigitalOcean Employee badge](https://www.digitalocean.com/api/static-content/v1/images?src=%2F_next%2Fstatic%2Fmedia%2Fdo-logo.15453cf1.svg&width=32) [February 12, 2021](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe?comment=177699) Show less thats very comprehensive. out of all drop explanation … this is the best thank you \- johny ![JournalDev](https://www.gravatar.com/avatar/7c7a4546c8430a5a91a338a177bf8e7e8e153fe7eaee076acfa6d46029efef49?default=retro) [JournalDev](https://www.digitalocean.com/community/users/journaldev)![DigitalOcean Employee badge](https://www.digitalocean.com/api/static-content/v1/images?src=%2F_next%2Fstatic%2Fmedia%2Fdo-logo.15453cf1.svg&width=32) [May 11, 2021](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe?comment=177700) Show less Thank u bro, well explained in very simple way \- KHAJA MOINUDDIN KHAN [![Creative Commons](https://www.digitalocean.com/api/static-content/v1/images?src=%2F_next%2Fstatic%2Fmedia%2Fcreativecommons.c0a877f1.png&width=384)](https://creativecommons.org/licenses/by-nc-sa/4.0/)This work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License. ## Deploy on DigitalOcean Click below to sign up for DigitalOcean's virtual machines, Databases, and AIML products. [Sign up](https://cloud.digitalocean.com/registrations/new?refcode=f6fcd01aaffb) ## Popular Topics 1. [AI/ML](https://www.digitalocean.com/community/tags/ai-ml) 2. [Ubuntu](https://www.digitalocean.com/community/tags/ubuntu) 3. [Linux Basics](https://www.digitalocean.com/community/tags/linux-basics) 4. [JavaScript](https://www.digitalocean.com/community/tags/javascript) 5. [Python](https://www.digitalocean.com/community/tags/python) 6. [MySQL](https://www.digitalocean.com/community/tags/mysql) 7. [Docker](https://www.digitalocean.com/community/tags/docker) 8. [Kubernetes](https://www.digitalocean.com/community/tags/kubernetes) 9. [All tutorials](https://www.digitalocean.com/community/tutorials) 10. [Talk to an expert](https://www.digitalocean.com/company/contact/sales?referrer=tutorials) ### Connect on Discord Join the conversation in our Discord to connect with fellow developers [Visit Discord](https://discord.gg/digitalocean) ## Featured tutorials 1. [SOLID Design Principles Explained: Building Better Software Architecture](https://www.digitalocean.com/community/tutorials/s-o-l-i-d-the-first-five-principles-of-object-oriented-design) 2. [How To Remove Docker Images, Containers, and Volumes](https://www.digitalocean.com/community/tutorials/how-to-remove-docker-images-containers-and-volumes) 3. [How to Create a MySQL User and Grant Privileges (Step-by-Step)](https://www.digitalocean.com/community/tutorials/how-to-create-a-new-user-and-grant-permissions-in-mysql) - [All tutorials](https://www.digitalocean.com/community/tutorials) - [All topic tags](https://www.digitalocean.com/community/tags) ##### Join the Tech Talk - [Syntax](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#syntax) - [Constructing Sample DataFrames](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#constructing-sample-dataframes) - [Dropping All Rows with Missing Values](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#dropping-all-rows-with-missing-values) - [Dropping All Columns with Missing Values](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#dropping-all-columns-with-missing-values) - [Dropping Rows or Columns if \`all\` the Values are \`Null\` with \`how\`](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#dropping-rows-or-columns-if-all-the-values-are-null-with-how) - [Dropping Rows or Columns if a Threshold is Crossed with \`thresh\`](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#dropping-rows-or-columns-if-a-threshold-is-crossed-with-thresh) - [Dropping Rows or Columns for Specific \`subsets\`](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#dropping-rows-or-columns-for-specific-subsets) - [Changing the source DataFrame after Dropping Rows or Columns with \`inplace\`](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#changing-the-source-dataframe-after-dropping-rows-or-columns-with-inplace) - [Conclusion](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#conclusion) - ## Deploy on DigitalOcean Click below to sign up for DigitalOcean's virtual machines, Databases, and AIML products. [Sign up](https://cloud.digitalocean.com/registrations/new?refcode=f6fcd01aaffb) ## Popular Topics 1. [AI/ML](https://www.digitalocean.com/community/tags/ai-ml) 2. [Ubuntu](https://www.digitalocean.com/community/tags/ubuntu) 3. [Linux Basics](https://www.digitalocean.com/community/tags/linux-basics) 4. [JavaScript](https://www.digitalocean.com/community/tags/javascript) 5. [Python](https://www.digitalocean.com/community/tags/python) 6. [MySQL](https://www.digitalocean.com/community/tags/mysql) 7. [Docker](https://www.digitalocean.com/community/tags/docker) 8. [Kubernetes](https://www.digitalocean.com/community/tags/kubernetes) 9. [All tutorials](https://www.digitalocean.com/community/tutorials) 10. [Talk to an expert](https://www.digitalocean.com/company/contact/sales?referrer=tutorials) ### Connect on Discord Join the conversation in our Discord to connect with fellow developers [Visit Discord](https://discord.gg/digitalocean) ## Featured tutorials 1. [SOLID Design Principles Explained: Building Better Software Architecture](https://www.digitalocean.com/community/tutorials/s-o-l-i-d-the-first-five-principles-of-object-oriented-design) 2. [How To Remove Docker Images, Containers, and Volumes](https://www.digitalocean.com/community/tutorials/how-to-remove-docker-images-containers-and-volumes) 3. [How to Create a MySQL User and Grant Privileges (Step-by-Step)](https://www.digitalocean.com/community/tutorials/how-to-create-a-new-user-and-grant-permissions-in-mysql) - [All tutorials](https://www.digitalocean.com/community/tutorials) - [All topic tags](https://www.digitalocean.com/community/tags) ![](https://www.digitalocean.com/api/static-content/v1/images?src=%2F_next%2Fstatic%2Fmedia%2Ftutorials-2-tulip.764b9f59.svg&width=1920) ## Become a contributor for community Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation. [Sign Up](https://www.digitalocean.com/community/pages/write-for-digitalocean) ![](https://www.digitalocean.com/api/static-content/v1/images?src=%2F_next%2Fstatic%2Fmedia%2Fdocs-2-kiwi.239a03ef.svg&width=1920) ## DigitalOcean Documentation Full documentation for every DigitalOcean product. [Learn more](https://docs.digitalocean.com/) ![](https://www.digitalocean.com/api/static-content/v1/images?src=%2F_next%2Fstatic%2Fmedia%2Fblogs-1-lavender.495d1f00.svg&width=1920) ## Resources for startups and AI-native businesses The Wave has everything you need to know about building a business, from raising funding to marketing your product. [Learn more](https://www.digitalocean.com/resources) ## Get our newsletter Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter. New accounts only. By submitting your email you agree to our [Privacy Policy](https://www.digitalocean.com/legal/privacy-policy) ## The developer cloud Scale up as you grow — whether you're running one virtual machine or ten thousand. [View all products](https://www.digitalocean.com/products) ![](https://www.digitalocean.com/api/static-content/v1/images?src=%2F_next%2Fstatic%2Fmedia%2Fclouds-mobile.5d14bead.svg&width=3840) ## Start building today From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications. [Sign up](https://cloud.digitalocean.com/registrations/new) ![](https://www.digitalocean.com/api/static-content/v1/images?src=%2F_next%2Fstatic%2Fmedia%2Fwaves-mobile.a054c63e.svg&width=3840) ## Company - [About](https://www.digitalocean.com/about) - [Leadership](https://www.digitalocean.com/leadership/executive-management) - [Blog](https://www.digitalocean.com/blog) - [Careers](https://www.digitalocean.com/careers) - [Customers](https://www.digitalocean.com/customers) - [Partners](https://www.digitalocean.com/partners) - [Referral Program](https://www.digitalocean.com/referral-program) - [Affiliate Program](https://www.digitalocean.com/affiliates) - [Press](https://www.digitalocean.com/press) - [Legal](https://www.digitalocean.com/legal) - [Privacy Policy](https://www.digitalocean.com/legal/privacy-policy) - [Security](https://www.digitalocean.com/security) - [Investor Relations](https://investors.digitalocean.com/) ## Products - [Gradient™ AI GPU Droplets](https://www.digitalocean.com/products/gradient/gpu-droplets) - [Gradient™ AI Bare Metal GPUs](https://www.digitalocean.com/products/gradient/bare-metal-gpus) - [Gradient™ AI 1-Click Models](https://www.digitalocean.com/products/gradient/1-click-models) - [Gradient™ AI Platform](https://www.digitalocean.com/products/gradient/platform) - [Droplets](https://www.digitalocean.com/products/droplets) - [Kubernetes](https://www.digitalocean.com/products/kubernetes) - [Functions](https://www.digitalocean.com/products/functions) - [App Platform](https://www.digitalocean.com/products/app-platform) - [Load Balancers](https://www.digitalocean.com/products/load-balancers) - [Managed Databases](https://www.digitalocean.com/products/managed-databases) - [Spaces](https://www.digitalocean.com/products/spaces) - [Block Storage](https://www.digitalocean.com/products/block-storage) - [Network File Storage](https://www.digitalocean.com/products/storage/network-file-storage) - [API](https://docs.digitalocean.com/reference/api) - [Uptime](https://www.digitalocean.com/products/uptime-monitoring) - [Cloud Security Posture Management (CSPM)](https://www.digitalocean.com/products/cloud-security-posture-management) - [Identity and Access Management (IAM)](https://www.digitalocean.com/products/identity-access-management) - [Cloudways](https://www.digitalocean.com/products/cloudways) - [View all Products](https://www.digitalocean.com/products) ## Resources - [Community Tutorials](https://www.digitalocean.com/community/tutorials) - [Community Q\&A](https://www.digitalocean.com/community/questions) - [CSS-Tricks](https://css-tricks.com/) - [Write for DOnations](https://www.digitalocean.com/community/pages/write-for-digitalocean) - [Currents Research](https://www.digitalocean.com/currents) - [DigitalOcean Startups](https://www.digitalocean.com/startups) - [Wavemakers Program](https://www.digitalocean.com/wavemakers) - [Compass Council](https://www.digitalocean.com/research) - [Open Source](https://www.digitalocean.com/open-source) - [Newsletter Signup](https://www.digitalocean.com/community#iaan) - [Marketplace](https://www.digitalocean.com/products/marketplace) - [Pricing](https://www.digitalocean.com/pricing) - [Pricing Calculator](https://www.digitalocean.com/pricing/calculator) - [Documentation](https://docs.digitalocean.com/) - [Release Notes](https://docs.digitalocean.com/release-notes) - [Code of Conduct](https://www.digitalocean.com/community/pages/code-of-conduct) - [Shop Swag](http://store.digitalocean.com/) ## Solutions - [AI GPU Hosting](https://www.digitalocean.com/solutions/ai-gpu-hosting) - [H100 Cloud GPU](https://www.digitalocean.com/solutions/h100-cloud-gpu) - [AI Training GPU](https://www.digitalocean.com/solutions/ai-training-gpu) - [GPU Inference](https://www.digitalocean.com/solutions/gpu-inference) - [VPS Hosting](https://www.digitalocean.com/solutions/vps-hosting) - [Website Hosting](https://www.digitalocean.com/solutions/website-hosting) - [VPN](https://www.digitalocean.com/solutions/vpn) - [Docker Hosting](https://www.digitalocean.com/solutions/docker-hosting) - [Node.js Hosting](https://www.digitalocean.com/solutions/nodejs-hosting) - [Web Mobile Apps](https://www.digitalocean.com/solutions/web-mobile-apps) - [WordPress Hosting](https://www.digitalocean.com/solutions/wordpress-hosting) - [Virtual Machines](https://www.digitalocean.com/solutions/virtual-machines) - [View all Solutions](https://www.digitalocean.com/solutions) ## Contact - [Support](https://www.digitalocean.com/support) - [Sales](https://www.digitalocean.com/company/contact/sales?referrer=footer) - [Report Abuse](https://www.digitalocean.com/company/contact/abuse) - [System Status](https://status.digitalocean.com/) - [Share your ideas](https://ideas.digitalocean.com/) ## Company - [About](https://www.digitalocean.com/about) - [Leadership](https://www.digitalocean.com/leadership/executive-management) - [Blog](https://www.digitalocean.com/blog) - [Careers](https://www.digitalocean.com/careers) - [Customers](https://www.digitalocean.com/customers) - [Partners](https://www.digitalocean.com/partners) - [Referral Program](https://www.digitalocean.com/referral-program) - [Affiliate Program](https://www.digitalocean.com/affiliates) - [Press](https://www.digitalocean.com/press) - [Legal](https://www.digitalocean.com/legal) - [Privacy Policy](https://www.digitalocean.com/legal/privacy-policy) - [Security](https://www.digitalocean.com/security) - [Investor Relations](https://investors.digitalocean.com/) ## Products - [Gradient™ AI GPU Droplets](https://www.digitalocean.com/products/gradient/gpu-droplets) - [Gradient™ AI Bare Metal GPUs](https://www.digitalocean.com/products/gradient/bare-metal-gpus) - [Gradient™ AI 1-Click Models](https://www.digitalocean.com/products/gradient/1-click-models) - [Gradient™ AI Platform](https://www.digitalocean.com/products/gradient/platform) - [Droplets](https://www.digitalocean.com/products/droplets) - [Kubernetes](https://www.digitalocean.com/products/kubernetes) - [Functions](https://www.digitalocean.com/products/functions) - [App Platform](https://www.digitalocean.com/products/app-platform) - [Load Balancers](https://www.digitalocean.com/products/load-balancers) - [Managed Databases](https://www.digitalocean.com/products/managed-databases) - [Spaces](https://www.digitalocean.com/products/spaces) - [Block Storage](https://www.digitalocean.com/products/block-storage) - [Network File Storage](https://www.digitalocean.com/products/storage/network-file-storage) - [API](https://docs.digitalocean.com/reference/api) - [Uptime](https://www.digitalocean.com/products/uptime-monitoring) - [Cloud Security Posture Management (CSPM)](https://www.digitalocean.com/products/cloud-security-posture-management) - [Identity and Access Management (IAM)](https://www.digitalocean.com/products/identity-access-management) - [Cloudways](https://www.digitalocean.com/products/cloudways) - [View all Products](https://www.digitalocean.com/products) ## Resources - [Community Tutorials](https://www.digitalocean.com/community/tutorials) - [Community Q\&A](https://www.digitalocean.com/community/questions) - [CSS-Tricks](https://css-tricks.com/) - [Write for DOnations](https://www.digitalocean.com/community/pages/write-for-digitalocean) - [Currents Research](https://www.digitalocean.com/currents) - [DigitalOcean Startups](https://www.digitalocean.com/startups) - [Wavemakers Program](https://www.digitalocean.com/wavemakers) - [Compass Council](https://www.digitalocean.com/research) - [Open Source](https://www.digitalocean.com/open-source) - [Newsletter Signup](https://www.digitalocean.com/community#iaan) - [Marketplace](https://www.digitalocean.com/products/marketplace) - [Pricing](https://www.digitalocean.com/pricing) - [Pricing Calculator](https://www.digitalocean.com/pricing/calculator) - [Documentation](https://docs.digitalocean.com/) - [Release Notes](https://docs.digitalocean.com/release-notes) - [Code of Conduct](https://www.digitalocean.com/community/pages/code-of-conduct) - [Shop Swag](http://store.digitalocean.com/) ## Solutions - [AI GPU Hosting](https://www.digitalocean.com/solutions/ai-gpu-hosting) - [H100 Cloud GPU](https://www.digitalocean.com/solutions/h100-cloud-gpu) - [AI Training GPU](https://www.digitalocean.com/solutions/ai-training-gpu) - [GPU Inference](https://www.digitalocean.com/solutions/gpu-inference) - [VPS Hosting](https://www.digitalocean.com/solutions/vps-hosting) - [Website Hosting](https://www.digitalocean.com/solutions/website-hosting) - [VPN](https://www.digitalocean.com/solutions/vpn) - [Docker Hosting](https://www.digitalocean.com/solutions/docker-hosting) - [Node.js Hosting](https://www.digitalocean.com/solutions/nodejs-hosting) - [Web Mobile Apps](https://www.digitalocean.com/solutions/web-mobile-apps) - [WordPress Hosting](https://www.digitalocean.com/solutions/wordpress-hosting) - [Virtual Machines](https://www.digitalocean.com/solutions/virtual-machines) - [View all Solutions](https://www.digitalocean.com/solutions) ## Contact - [Support](https://www.digitalocean.com/support) - [Sales](https://www.digitalocean.com/company/contact/sales?referrer=footer) - [Report Abuse](https://www.digitalocean.com/company/contact/abuse) - [System Status](https://status.digitalocean.com/) - [Share your ideas](https://ideas.digitalocean.com/) © 2026 DigitalOcean, LLC.[Sitemap](https://www.digitalocean.com/sitemap).[Cookie Preferences]() This site uses cookies and related technologies, as described in our [privacy policy](https://www.digitalocean.com/legal/privacy-policy/), for purposes that may include site operation, analytics, enhanced user experience, or advertising. You may choose to consent to our use of these technologies, or manage your own preferences. Please visit our [cookie policy](https://www.digitalocean.com/legal/cookie-policy) for more information. Agree & Proceed Decline All Manage Choices Loading... ## Community ## Product Docs ## Marketplace ## DigitalOcean Blog navigate go exit
Readable Markdown
### [Introduction](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#introduction) In this tutorial, you’ll learn how to use panda’s DataFrame `dropna()` function. `NA` values are “Not Available”. This can apply to `Null`, `None`, `pandas.NaT`, or `numpy.nan`. Using `dropna()` will drop the rows and columns with these values. This can be beneficial to provide you with only valid data. By default, this function returns a new DataFrame and the source DataFrame remains unchanged. This tutorial was verified with Python 3.10.9, pandas 1.5.2, and NumPy 1.24.1. ## [Syntax](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#syntax) `dropna()` takes the following parameters: ``` dropna(self, axis=0, how="any", thresh=None, subset=None, inplace=False) ``` - `axis`: `{0 (or 'index'), 1 (or 'columns')}, default 0` - If `0`, drop rows with missing values. - If `1`, drop columns with missing values. - `how`: `{'any', 'all'}, default 'any'` - If `'any'`, drop the row or column if any of the values is `NA`. - If `'all'`, drop the row or column if all of the values are `NA`. - `thresh`: (optional) an `int` value to specify the threshold for the drop operation. - `subset`: (optional) column label or sequence of labels to specify rows or columns. - `inplace`: (optional) a `bool` value. - If `True`, the source DataFrame is changed and `None` is returned. ## [Constructing Sample DataFrames](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#constructing-sample-dataframes) Construct a sample DataFrame that contains valid and invalid values: dropnaExample.py ``` import pandas as pd import numpy as np d1 = { 'Name': ['Shark', 'Whale', 'Jellyfish', 'Starfish'], 'ID': [1, 2, 3, 4], 'Population': [100, 200, np.nan, pd.NaT], 'Regions': [1, None, pd.NaT, pd.NaT] } df1 = pd.DataFrame(d1) print(df1) ``` This code will print out the DataFrame: ``` OutputName ID Population Regions 0 Shark 1 100 1 1 Whale 2 200 None 2 Jellyfish 3 NaN NaT 3 Starfish 4 NaT NaT ``` Then add a second DataFrame with additional rows and columns with `NA` values: ``` d2 = { 'Name': ['Shark', 'Whale', 'Jellyfish', 'Starfish', pd.NaT], 'ID': [1, 2, 3, 4, pd.NaT], 'Population': [100, 200, np.nan, pd.NaT, pd.NaT], 'Regions': [1, None, pd.NaT, pd.NaT, pd.NaT], 'Endangered': [pd.NaT, pd.NaT, pd.NaT, pd.NaT, pd.NaT] } df2 = pd.DataFrame(d2) print(df2) ``` This will output a new DataFrame: ``` OutputName ID Population Regions Endangered 0 Shark 1 100 1 NaT 1 Whale 2 200 None NaT 2 Jellyfish 3 NaN NaT NaT 3 Starfish 4 NaT NaT NaT 4 NaT NaT NaT NaT NaT ``` You will use the preceding DataFrames in the examples that follow. ## [Dropping All Rows with Missing Values](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#dropping-all-rows-with-missing-values) Use `dropna()` to remove rows with any `None`, `NaN`, or `NaT` values: dropnaExample.py ``` dfresult = df1.dropna() print(dfresult) ``` This will output: ``` OutputName ID Population Regions 0 Shark 1 100 1 ``` A new DataFrame with a single row that didn’t contain any `NA` values. ## [Dropping All Columns with Missing Values](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#dropping-all-columns-with-missing-values) Use `dropna()` with `axis=1` to remove columns with any `None`, `NaN`, or `NaT` values: ``` dfresult = df1.dropna(axis=1) print(dfresult) ``` The columns with any `None`, `NaN`, or `NaT` values will be dropped: ``` OutputName ID 0 Shark 1 1 Whale 2 2 Jellyfish 3 3 Starfish 4 ``` A new DataFrame with a single column that contained non-`NA` values. ## [Dropping Rows or Columns if `all` the Values are `Null` with `how`](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#dropping-rows-or-columns-if-all-the-values-are-null-with-how) Use the second DataFrame and `how`: dropnaExample.py ``` dfresult = df2.dropna(how='all') print(dfresult) ``` The rows with `all` values equal to `NA` will be dropped: ``` OutputName ID Population Regions Endangered 0 Shark 1 100 1 NaT 1 Whale 2 200 None NaT 2 Jellyfish 3 NaN NaT NaT 3 Starfish 4 NaT NaT NaT ``` The fifth row was dropped. Next, use `how` and specify the `axis`: dropnaExample.py ``` dfresult = df2.dropna(how='all', axis=1) print(dfresult) ``` The columns with `all` values equal to `NA` will be dropped: ``` OutputName ID Population Regions 0 Shark 1 100 1 1 Whale 2 200 None 2 Jellyfish 3 NaN NaT 3 Starfish 4 NaT NaT 4 NaT NaT NaT NaT ``` The fifth column was dropped. ## [Dropping Rows or Columns if a Threshold is Crossed with `thresh`](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#dropping-rows-or-columns-if-a-threshold-is-crossed-with-thresh) Use the second DataFrame with `thresh` to drop rows that do not meet the threshold of at least `3` non-`NA` values: dropnaExample.py ``` dfresult = df2.dropna(thresh=3) print(dfresult) ``` The rows do not have at least `3` non-`NA` will be dropped: ``` OutputName ID Population Regions Endangered 0 Shark 1 100 1 NaT 1 Whale 2 200 None NaT ``` The third, fourth, and fifth rows were dropped. ## [Dropping Rows or Columns for Specific `subsets`](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#dropping-rows-or-columns-for-specific-subsets) Use the second DataFrame with `subset` to drop rows with `NA` values in the `Population` column: dropnaExample.py ``` dfresult = df2.dropna(subset=['Population']) print(dfresult) ``` The rows that have `Population` with `NA` values will be dropped: ``` OutputName ID Population Regions Endangered 0 Shark 1 100 1 NaT 1 Whale 2 200 None NaT ``` The third, fourth, and fifth rows were dropped. You can also specify the `index` values in the `subset` when dropping columns from the DataFrame: dropnaExample.py ``` dfresult = df2.dropna(subset=[1, 2], axis=1) print(dfresult) ``` The columns that contain `NA` values in subset of rows `1` and `2`: ``` OutputName ID 0 Shark 1 1 Whale 2 2 Jellyfish 3 3 Starfish 4 4 NaT NaT ``` The third, fourth, and fifth columns were dropped. ## [Changing the source DataFrame after Dropping Rows or Columns with `inplace`](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#changing-the-source-dataframe-after-dropping-rows-or-columns-with-inplace) By default, `dropna()` does not modify the source DataFrame. However, in some cases, you may wish to save memory when working with a large source DataFrame by using `inplace`. dropnaExample.py ``` df1.dropna(inplace=True) print(df1) ``` This code does not use a `dfresult` variable. This will output: ``` OutputName ID Population Regions 0 Shark 1 100 1 ``` The original DataFrame has been modified. ## [Conclusion](https://www.digitalocean.com/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe#conclusion) In this article, you used the `dropna()` function to remove rows and columns with `NA` values. Continue your learning with more Python and pandas tutorials - [Python pandas Module Tutorial](https://www.digitalocean.com/community/tutorials/python-pandas-module-tutorial), [pandas Drop Duplicate Rows](https://www.digitalocean.com/community/tutorials/pandas-drop-duplicate-rows-drop_duplicates-function). **References** - [pandas DataFrame `dropna()` API Doc](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.dropna.html) Was this helpful? [![Creative Commons](https://www.digitalocean.com/api/static-content/v1/images?src=%2F_next%2Fstatic%2Fmedia%2Fcreativecommons.c0a877f1.png&width=384)](https://creativecommons.org/licenses/by-nc-sa/4.0/)This work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Shard83 (laksa)
Root Hash13428457316079428483
Unparsed URLcom,digitalocean!www,/community/tutorials/pandas-dropna-drop-null-na-values-from-dataframe s443