Pandas df.dropna() function
df.dropna() is a function in Pandas used to remove rows or columns that contain missing values.
In data analysis, missing values (NaN、None or NULL) are a common problem.dropna() The function provides a simple way to clean data: you can choose to remove rows or columns that contain missing values, or filter data based on the proportion of missing values.
Basic syntax and parameters
dropna() is a member function of DataFrame, called via the dot operator . to call it.
Syntax format
DataFrame.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False)
Parameter Description
| Parameters | Type | Required | Description | Default value |
|---|---|---|---|---|
| axis | int or str | Optional | Specifies whether to remove rows or columns.0 or 'index' indicates removing rows that contain missing values;1 or 'columns' indicates removing columns that contain missing values. |
0 |
| how | str | Optional | Specifies the removal condition.'any' indicates removing as long as there is any missing value;'all' indicates removing only when all values are missing. |
'any' |
| thresh | int | Optional | Sets the minimum number of non-missing values. If the number of non-missing values in a row (or column) is less than thresh, that row (or column) is removed. |
None |
| subset | array-like | Optional | Specifies in which columns (or rows) to check for missing values. If axis=0, then subset specifies column names; if axis=1, then subset specifies row indices. |
None |
| inplace | bool | Optional | If it is True, modify the original DataFrame directly without returning a new object; if False, a new DataFrame is returned and the original data is unchanged. |
False |
Return value description
- Returns a new DataFrame (if
inplace=False), orNone(ifinplace=True)。 - The returned DataFrame does not contain rows or columns with missing values.
Example
Let us thoroughly master dropna() through a series of examples from simple to complex.
Example 1: Removing rows with missing values
This is the most common usage, removing any row that contains missing values.
Example
import
pandas
as
pd
import
numpy
as
np
Create a DataFrame with missing values
data
=
{
'Name'
:
[
'Zhang San'
,
'Li Si'
,
'Wang Wu'
,
'Zhao Liu'
,
'Qian Qi'
]
,
'Age'
:
[
25
,
30
,
np.
nan
,
35
,
28
]
,
Wang Wu's age is missing
'Salary'
:
[
5000
,
6000
,
5500
,
np.
nan
,
7000
]
,
Zhao Liu's salary is missing
'Department'
:
[
'Ji shu' (a Chinese phrase)
,
'Marketing'
,
'Ji shu' (a Chinese phrase)
,
'Marketing'
,
'Ji shu' (a Chinese phrase)
]
}
df
=
pd.
DataFrame
(
data
)
(
"Original data:"
)
(
df
)
(
"="
50
)
Remove rows with missing values (default behavior)
df_cleaned
=
df.
dropna
(
)
(
"Data after removing missing values:"
)
(
df_cleaned
)
Expected output:
原始数据:
姓名 年龄 薪资 部门
0 张三 25.0 5000.0 技术
1 李四 30.0 6000.0 市场
2 王五 NaN 5500.0 技术
3 赵六 35.0 NaN 市场
4 钱七 28.0 7000.0 技术
==================================================
Data after deleting missing values:
姓名 年龄 薪资 部门
0 张三 25.0 5000.0 技术
1 李四 30.0 6000.0 市场
4 钱七 28.0 7000.0 技术
Code analysis:
- We created a DataFrame with missing values, where Wang Wu's age is
NaN, and Zhao Liu's salary isNaN。 - Use
df.dropna()the default parameters, removing all rows with missing values. - The result keeps the data for Zhang San, Li Si, and Qian Qi, and removes Wang Wu and Zhao Liu.
Example 2: Removing columns with missing values
Sometimes we care more about which columns have missing values, so we can choose to remove columns.
Example
import
pandas
as
pd
import
numpy
as
np
Create a DataFrame with missing values
data
=
{
'Name'
:
[
'Zhang San'
,
'Li Si'
,
'Wang Wu'
,
'Zhao Liu'
]
,
'Age'
:
[
25
,
30
,
35
,
40
]
,
'Salary'
:
[
5000
,
np.
nan
,
5500
,
np.
nan
]
,
Multiple columns have missing values
'Department'
:
[
np.
nan
,
'Marketing'
,
'Ji shu' (a Chinese phrase)
,
'Marketing'
]
,
'City'
:
[
'Beijing'
,
'Shanghai'
,
'Guangzhou'
,
'Shenzhen'
]
No missing values
}
df
=
pd.
DataFrame
(
data
)
(
"Original data:"
)
(
df
)
(
"="
50
)
Remove columns with missing values
df_cleaned
=
df.
dropna
(
axis
=
1
)
(
"Data after removing columns with missing values:"
)
(
df_cleaned
)
Expected output:
原始数据:
姓名 年龄 薪资 部门 城市
0 张三 25 5000.0 NaN 北京
1 李四 30 NaN 市场 上海
2 王五 35 5500.0 技术 广州
3 赵六 40 NaN 市场 深圳
==================================================
Data after deleting columns with missing values:
姓名 年龄 城市
0 张三 25 北京
1 李四 30 上海
2 王五 35 广州
3 赵六 40 深圳
Code analysis:
- Settings
axis=1indicates operating by column. - The Salary and Department columns contain missing values, so they are removed.
- The Name, Age, and City columns have no missing values and are kept.
Example 3: Using the thresh parameter to control the removal condition
thresh The parameter lets you set the minimum number of non-missing values; removal happens only when the number of non-missing values in a row (or column) is below this threshold.
Example
import
pandas
as
pd
import
numpy
as
np
Create a DataFrame where every row has some missing values
data
=
{
'A'
:
[
1
,
2
,
np.
nan
,
4
,
5
]
,
'B'
:
[
np.
nan
,
2
,
3
,
4
,
np.
nan
]
,
has 2 missing values
'C'
:
[
1
,
2
,
3
,
4
,
5
]
,
No missing values
'D'
:
[
np.
nan
,
np.
nan
,
np.
nan
,
4
,
5
]
has 3 missing values
}
df
=
pd.
DataFrame
(
data
)
(
"Original data:"
)
(
df
)
(
"="
50
)
Keep only rows with at least 3 non-missing values
df_cleaned
=
df.
dropna
(
thresh
=
3
)
(
"Rows with at least 3 non-missing values:"
)
(
df_cleaned
)
Expected output:
原始数据:
A B C D
0 1.0 NaN 1.0 NaN
1 2.0 2.0 2.0 NaN
2 NaN 3.0 3.0 NaN
3 4.0 4.0 4.0 4.0
4 5.0 NaN 5.0 5.0
==================================================
Keep rows with at least 3 non-missing values:
A B C D
1 2.0 2.0 2.0 NaN
3 4.0 4.0 4.0 4.0
Code analysis:
- Row 0 has only 1 non-missing value (1.0) and is removed.
- Row 1 has 3 non-missing values (2.0, 2.0, 2.0) and is kept.
- Row 2 has only 2 non-missing values (3.0, 3.0) and is removed.
- Row 3 has 4 non-missing values and is kept.
- Row 4 has only 3 non-missing values (5.0, 5.0, 5.0) and is kept.
Example 4: Using subset to specify columns to check
Sometimes we only care whether specific columns have missing values; we can use subset parameter was added.
Example
import
pandas
as
pd
import
numpy
as
np
Create a DataFrame
data
=
{
'Name'
:
[
'Zhang San'
,
'Li Si'
,
'Wang Wu'
,
'Zhao Liu'
]
,
'Age'
:
[
25
,
np.
nan
,
35
,
40
]
,
Li Si's age is missing
'Salary'
:
[
5000
,
6000
,
np.
nan
,
8000
]
,
Wang Wu's salary is missing
'Department'
:
[
'Ji shu' (a Chinese phrase)
,
'Marketing'
,
'Ji shu' (a Chinese phrase)
,
'Marketing'
]
}
df
=
pd.
DataFrame
(
data
)
(
"Original data:"
)
(
df
)
(
"="
50
)
Check for missing values only in the "Name" and "Department" columns
df_cleaned
=
df.
dropna
(
subset
=
[
'Name'
,
'Department'
]
)
(
"Data after checking for missing values only in the Name and Department columns:"
)
(
df_cleaned
)
(
"="
50
)
Check for missing values only in the "Salary" column
df_cleaned2
=
df.
dropna
(
subset
=
[
'Salary'
]
)
(
"Data after checking for missing values only in the Salary column:"
)
(
df_cleaned2
)
Expected output:
原始数据:
姓名 年龄 薪资 部门
0 张三 25.0 5000.0 技术
1 李四 NaN 6000.0 市场
2 王五 35.0 NaN 技术
3 赵六 40.0 8000.0 市场
==================================================
Data after checking for missing values only in the Name and Department columns:
姓名 年龄 薪资 部门
0 张三 25.0 5000.0 技术
1 李四 NaN 6000.0 市场
2 王五 35.0 NaN 技术
3 赵六 40.0 8000.0 市场
==================================================
Data after checking for missing values only in the salary column:
姓名 年龄 薪资 部门
0 张三 25.0 5000.0 技术
1 李四 30.0 6000.0 市场
3 赵六 40.0 8000.0 市场
Code analysis:
- The Name and Department columns have no missing values, so all rows are kept after the check.
- In the Salary column, Wang Wu's salary is
NaN, so row 2 is removed.
Example 5: Using how='all' to remove only rows where all values are missing
how='all' The parameter indicates removal only when all values in a row (or column) are missing.
Example
import
pandas
as
pd
import
numpy
as
np
Create a DataFrame containing some rows where all values are missing
data
=
{
'A'
:
[
1
,
np.
nan
,
np.
nan
,
4
]
,
'B'
:
[
2
,
np.
nan
,
np.
nan
,
5
]
,
'C'
:
[
3
,
np.
nan
,
np.
nan
,
6
]
}
df
=
pd.
DataFrame
(
data
)
(
"Original data:"
)
(
df
)
(
"="
50
)
Remove rows where all values are missing
df_cleaned
=
df.
dropna
(
how
=
'all'
)
(
"After removing rows where all values are missing:"
)
(
df_cleaned
)
Expected output:
原始数据:
A B C
0 1.0 2.0 3.0
1 NaN NaN NaN
2 NaN NaN NaN
3 4.0 5.0 6.0
==================================================
After deleting rows that are entirely missing values:
A B C
0 1.0 2.0 3.0
3 4.0 5.0 6.0
Code analysis:
- All values in row 1 and row 2 are
NaN, usinghow='all'Remove them. - Rows 0 and 3 have actual data and are kept.
Notes
dropna()By default the original DataFrame is not modified; to modify it in place, useinplace=Trueparameter was added.- Use
threshWhen using the parameter, the value set should be based on your data quality requirements. - Before removing data, it is recommended to first use
df.isnull().sum()to view the number of missing values in each column and make a more informed decision. - For important datasets, use
dropna()with caution, because it may lead to significant data loss; you can consider usingfillna()to fill in missing values.
Common Pandas functions