Sometimes data has empty spots called missing values. We use dropna() to remove those spots so our data is clean and ready to use.
0
0
Dropping missing values with dropna() in Pandas
Introduction
When you have a table with empty cells and want to remove rows or columns that have them.
When preparing data for analysis or machine learning and missing values can cause errors.
When you want to focus only on complete data without any gaps.
When cleaning survey results where some answers are missing.
When you want to quickly remove incomplete records from your dataset.
Syntax
Pandas
DataFrame.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False)
axis=0 means drop rows; axis=1 means drop columns.
how='any' drops if any missing value is found; how='all' drops only if all values are missing.
Examples
Drop all rows that have any missing value.
Pandas
df.dropna()
Drop all columns that have any missing value.
Pandas
df.dropna(axis=1)Drop rows only if all values in the row are missing.
Pandas
df.dropna(how='all')Drop rows where
Age or Salary columns have missing values.Pandas
df.dropna(subset=['Age', 'Salary'])
Sample Program
This code creates a table with some missing ages and salaries. Then it removes any row that has a missing value and shows the cleaned table.
Pandas
import pandas as pd # Create a sample DataFrame with missing values data = { 'Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Age': [25, None, 30, None], 'Salary': [50000, 60000, None, 45000] } df = pd.DataFrame(data) print('Original DataFrame:') print(df) # Drop rows with any missing values clean_df = df.dropna() print('\nDataFrame after dropping rows with missing values:') print(clean_df)
OutputSuccess
Important Notes
Using inplace=True changes the original DataFrame without needing to assign it again.
Be careful: dropping too many rows can reduce your data size a lot.
You can choose to drop columns instead of rows by setting axis=1.
Summary
dropna() removes rows or columns with missing values.
You can control whether to drop rows or columns using axis.
Use how to decide if any or all missing values trigger dropping.