Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pandas df.dropna() function

Pandas df.dropna() function

Pandas Common Functions Common Pandas functions


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


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

)

print

(

"Original data:"

)

print

(

df

)

print

(

"="

50

)

Remove rows with missing values (default behavior)

df_cleaned

=

df.

dropna

(

)

print

(

"Data after removing missing values:"

)

print

(

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:

  1. We created a DataFrame with missing values, where Wang Wu's age is NaN, and Zhao Liu's salary is NaN。
  2. Use df.dropna() the default parameters, removing all rows with missing values.
  3. 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

)

print

(

"Original data:"

)

print

(

df

)

print

(

"="

50

)

Remove columns with missing values

df_cleaned

=

df.

dropna

(

axis

=

1

)

print

(

"Data after removing columns with missing values:"

)

print

(

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:

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

)

print

(

"Original data:"

)

print

(

df

)

print

(

"="

50

)

Keep only rows with at least 3 non-missing values

df_cleaned

=

df.

dropna

(

thresh

=

3

)

print

(

"Rows with at least 3 non-missing values:"

)

print

(

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:

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

)

print

(

"Original data:"

)

print

(

df

)

print

(

"="

50

)

Check for missing values only in the "Name" and "Department" columns

df_cleaned

=

df.

dropna

(

subset

=

[

'Name'

,

'Department'

]

)

print

(

"Data after checking for missing values only in the Name and Department columns:"

)

print

(

df_cleaned

)

print

(

"="

50

)

Check for missing values only in the "Salary" column

df_cleaned2

=

df.

dropna

(

subset

=

[

'Salary'

]

)

print

(

"Data after checking for missing values only in the Salary column:"

)

print

(

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:

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

)

print

(

"Original data:"

)

print

(

df

)

print

(

"="

50

)

Remove rows where all values are missing

df_cleaned

=

df.

dropna

(

how

=

'all'

)

print

(

"After removing rows where all values are missing:"

)

print

(

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:


Notes


Pandas Common Functions Common Pandas functions