Pandas 删除带有 NaN 的行

Suraj Joshi 2023年1月30日 2021年1月22日
  1. Pandas 使用 DataFrame.notna() 方法删除带有 NaN 的行
  2. Pandas 使用 DataFrame.dropna() 方法只删除所有列都是 NaN 值的行
  3. Pandas 使用 DataFrame.dropna() 方法仅在某一列的值为 NaN 的情况下才删除行
  4. Pandas 使用 DataFrame.dropna() 方法删除任意列为 NaN 值的行
Pandas 删除带有 NaN 的行

本教程解释了我们如何使用 DataFrame.notna()DataFrame.dropna() 方法删除所有带有 NaN 值的行。

我们将在下面的示例代码中使用 DataFrame。

import pandas as pd

roll_no = [501, 502, 503, 504, 505]

data = pd.DataFrame({
    'Name': ['Alice', 'Steven', 'Neesham', 'Chris', 'Alice'],
    'Age':  [19, None, 18, 21, None],
    'Income($)': [4000, 5000, None, 3500, None],
    'Expense($)': [3000, 2000, 2500, 25000, None]

})

print(data)

输出:

      Name   Age  Income($)  Expense($)
0    Alice  19.0     4000.0      3000.0
1   Steven   NaN     5000.0      2000.0
2  Neesham  18.0        NaN      2500.0
3    Chris  21.0     3500.0     25000.0
4    Alice   NaN        NaN         NaN

Pandas 使用 DataFrame.notna() 方法删除带有 NaN 的行

DataFrame.notna() 方法返回一个布尔对象,其行数和列数与调用者 DataFrame 相同。如果元素不是 NaN,它将被映射到布尔对象中的 True 值,如果元素是 NaN,它将被映射到 False 值。

import pandas as pd

roll_no = [501, 502, 503, 504, 505]

data = pd.DataFrame({
    'Name': ['Alice', 'Steven', 'Neesham', 'Chris', 'Alice'],
    'Age':  [19, None, 18, 21, None],
    'Income($)': [4000, 5000, None, 3500, None],
    'Expense($)': [3000, 2000, 2500, 25000, None]

})
print("Initial DataFrame:")
print(data)

print("")

data = data[data['Income($)'].notna()]
print("DataFrame after removing rows with NaN value in Income Field:")
print(data)

输出:

Initial DataFrame:
      Name   Age  Income($)  Expense($)
0    Alice  19.0     4000.0      3000.0
1   Steven   NaN     5000.0      2000.0
2  Neesham  18.0        NaN      2500.0
3    Chris  21.0     3500.0     25000.0
4    Alice   NaN        NaN         NaN

DataFrame after removing rows with NaN value in Income Field:
     Name   Age  Income($)  Expense($)
0   Alice  19.0     4000.0      3000.0
1  Steven   NaN     5000.0      2000.0
3   Chris  21.0     3500.0     25000.0

这里,我们将 notna() 方法应用于 dataIncome($) 列,它将返回一个系列对象,根据该列的值,有 TrueFalse 值。当我们将布尔对象作为索引传递给原始 DataFrame 时,我们只得到 Income($) 列没有 NaN 值的行。

Pandas 使用 DataFrame.dropna() 方法只删除所有列都是 NaN 值的行

import pandas as pd

roll_no = [501, 502, 503, 504, 505]

data = pd.DataFrame({
    'Id': [621, 645, 210, 345, None],
    'Age':  [19, None, 18, 21, None],
    'Income($)': [4000, 5000, None, 3500, None],
    'Expense($)': [3000, 2000, 2500, 25000, None]

})
print("Initial DataFrame:")
print(data)

print("")

data = data.dropna(how='all')
print("DataFrame after removing rows with NaN value in All Columns:")
print(data)

输出:

Initial DataFrame:
      Id   Age  Income($)  Expense($)
0  621.0  19.0     4000.0      3000.0
1  645.0   NaN     5000.0      2000.0
2  210.0  18.0        NaN      2500.0
3  345.0  21.0     3500.0     25000.0
4    NaN   NaN        NaN         NaN

DataFrame after removing rows with NaN value in All Columns:
      Id   Age  Income($)  Expense($)
0  621.0  19.0     4000.0      3000.0
1  645.0   NaN     5000.0      2000.0
2  210.0  18.0        NaN      2500.0
3  345.0  21.0     3500.0     25000.0

它只删除 DataFrame 中所有字段中含有 NaN 值的行。我们在 dropna() 方法中设置 how='all',让该方法只在行的所有列值都是 NaN 时才删除行。

Pandas 使用 DataFrame.dropna() 方法仅在某一列的值为 NaN 的情况下才删除行

import pandas as pd

roll_no = [501, 502, 503, 504, 505]

data = pd.DataFrame({
    'Id': [621, 645, 210, 345, None],
    'Age':  [19, None, 18, 21, None],
    'Income($)': [4000, 5000, None, 3500, None],
    'Expense($)': [3000, 2000, 2500, 25000, None]

})
print("Initial DataFrame:")
print(data)

print("")

data = data.dropna(subset=["Id"])
print("DataFrame after removing rows with NaN value in Id Column:")
print(data)

输出:

Initial DataFrame:
      Id   Age  Income($)  Expense($)
0  621.0  19.0     4000.0      3000.0
1  645.0   NaN     5000.0      2000.0
2  210.0  18.0        NaN      2500.0
3  345.0  21.0     3500.0     25000.0
4    NaN   NaN        NaN         NaN

DataFrame after removing rows with NaN value in Id Column:
      Id   Age  Income($)  Expense($)
0  621.0  19.0     4000.0      3000.0
1  645.0   NaN     5000.0      2000.0
2  210.0  18.0        NaN      2500.0
3  345.0  21.0     3500.0     25000.0

它将删除 DataFrame 中所有仅在 Id 列中具有 NaN 值的列。

Pandas 使用 DataFrame.dropna() 方法删除任意列为 NaN 值的行

import pandas as pd

roll_no = [501, 502, 503, 504, 505]

data = pd.DataFrame({
    'Id': [621, 645, 210, 345, None],
    'Age':  [19, None, 18, 21, None],
    'Income($)': [4000, 5000, None, 3500, None],
    'Expense($)': [3000, 2000, 2500, 25000, None]

})
print("Initial DataFrame:")
print(data)

print("")

data = data.dropna()
print("DataFrame after removing rows with NaN value in any column:")
print(data)

输出:

Initial DataFrame:
      Id   Age  Income($)  Expense($)
0  621.0  19.0     4000.0      3000.0
1  645.0   NaN     5000.0      2000.0
2  210.0  18.0        NaN      2500.0
3  345.0  21.0     3500.0     25000.0
4    NaN   NaN        NaN         NaN

DataFrame after removing rows with NaN value in any column:
      Id   Age  Income($)  Expense($)
0  621.0  19.0     4000.0      3000.0
3  345.0  21.0     3500.0     25000.0

默认情况下,dropna() 方法将删除所有至少有一个 NaN 值的行。

Author: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn

相关文章 - Pandas DataFrame Row

相关文章 - Pandas NaN