Pandas是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具。Pandas提供了大量能使我们快速便捷地处理数据的函数和方法。你很快就会发现,它是使Python成为强大而高效的数据分析环境的重要因素之一,本文主要介绍Python Pandas 数据清理。

1、Data Cleaning

数据清理意味着修复数据集中的错误数据。

错误的数据可能是:

  • 空单元格
  • 数据格式错误
  • 资料错误
  • 重复数据

在本教程中,将学习如何处理所有这些问题。

2、示例数据集

示例数据集:

      Duration          Date  Number  Count Calories
  0         60  '2020/12/01'    110       130     409.1
  1         60  '2020/12/02'    117       145     479.0
  2         60  '2020/12/03'    103       135     340.0
  3         45  '2020/12/04'    109       175     282.4
  4         45  '2020/12/05'    117       148     406.0
  5         60  '2020/12/06'    102       127     300.0
  6         60  '2020/12/07'    110       136     374.0
  7        450  '2020/12/08'    104       134     253.3
  8         30  '2020/12/09'    109       133     195.1
  9         60  '2020/12/10'     98       124     269.0
  10        60  '2020/12/11'    103       147     329.3
  11        60  '2020/12/12'    100       120     250.7
  12        60  '2020/12/12'    100       120     250.7
  13        60  '2020/12/13'    106       128     345.3
  14        60  '2020/12/14'    104       132     379.3
  15        60  '2020/12/15'     98       123     275.0
  16        60  '2020/12/16'     98       120     215.2
  17        60  '2020/12/17'    100       120     300.0
  18        45  '2020/12/18'     90       112       NaN
  19        60  '2020/12/19'    103       123     323.0
  20        45  '2020/12/20'     97       125     243.0
  21        60  '2020/12/21'    108       131     364.2
  22        45           NaN    100       119     282.0
  23        60  '2020/12/23'    130       101     300.0
  24        45  '2020/12/24'    105       132     246.0
  25        60  '2020/12/25'    102       126     334.5
  26        60  '2020/12/26'    100       120     250.0
  27        60  '2020/12/27'     92       118     241.0
  28        60  '2020/12/28'    103       132       NaN
  29        60  '2020/12/29'    100       132     280.0
  30        60  '2020/12/30'    102       129     380.3
  31        60  '2020/12/31'     92       115     243.0

数据集包含一些空单元格 (22行中"Date"列, 及18和28行中"Calories"列)。
数据集包含错误的格式 (26行中"Date"列)。
数据集包含错误的数据 (7行中"Duration"列)。
数据集包含重复数据 ( 11和12行)。

3、Pandas 清洗空值

如要删除包含空字段的行,可以使用 dropna() 。还可以使用 fillna() 方法来替换一些空字段

参考文档:

Python pandas.DataFrame.dropna函数方法的使用

Python pandas.DataFrame.fillna函数方法的使用

1)删除包含空数据的行

import pandas as pd

df = pd.read_csv('data.csv')
# 默认情况下,dropna() 方法返回一个新的 DataFrame,不会修改源数据。
# 如需要修改源数据,则需要使用df.dropna(inplace = True)
new_df = df.dropna()

print(new_df.to_string())

输出结果:

      Duration          Date  Number  Count Calories
  0         60  '2020/12/01'    110       130     409.1
  1         60  '2020/12/02'    117       145     479.0
  2         60  '2020/12/03'    103       135     340.0
  3         45  '2020/12/04'    109       175     282.4
  4         45  '2020/12/05'    117       148     406.0
  5         60  '2020/12/06'    102       127     300.0
  6         60  '2020/12/07'    110       136     374.0
  7        450  '2020/12/08'    104       134     253.3
  8         30  '2020/12/09'    109       133     195.1
  9         60  '2020/12/10'     98       124     269.0
  10        60  '2020/12/11'    103       147     329.3
  11        60  '2020/12/12'    100       120     250.7
  12        60  '2020/12/12'    100       120     250.7
  13        60  '2020/12/13'    106       128     345.3
  14        60  '2020/12/14'    104       132     379.3
  15        60  '2020/12/15'     98       123     275.0
  16        60  '2020/12/16'     98       120     215.2
  17        60  '2020/12/17'    100       120     300.0
  18        60  '2020/12/19'    103       123     323.0
  19        45  '2020/12/20'     97       125     243.0
  20        60  '2020/12/21'    108       131     364.2
  21        60  '2020/12/23'    130       101     300.0
  22        45  '2020/12/24'    105       132     246.0
  23        60  '2020/12/25'    102       126     334.5
  24        60  '2020/12/26'    100       120     250.0
  25        60  '2020/12/27'     92       118     241.0
  26        60  '2020/12/29'    100       132     280.0
  27        60  '2020/12/30'    102       129     380.3
  28        60  '2020/12/31'     92       115     243.0    

2)移除指定列有空值的行

import pandas as pd

df = pd.read_csv('data.csv')

df.dropna(subset=['Calories'], inplace = True)

print(df.to_string())

输出结果:

      Duration          Date  Number  Count Calories
  0         60  '2020/12/01'    110       130     409.1
  1         60  '2020/12/02'    117       145     479.0
  2         60  '2020/12/03'    103       135     340.0
  3         45  '2020/12/04'    109       175     282.4
  4         45  '2020/12/05'    117       148     406.0
  5         60  '2020/12/06'    102       127     300.0
  6         60  '2020/12/07'    110       136     374.0
  7        450  '2020/12/08'    104       134     253.3
  8         30  '2020/12/09'    109       133     195.1
  9         60  '2020/12/10'     98       124     269.0
  10        60  '2020/12/11'    103       147     329.3
  11        60  '2020/12/12'    100       120     250.7
  12        60  '2020/12/12'    100       120     250.7
  13        60  '2020/12/13'    106       128     345.3
  14        60  '2020/12/14'    104       132     379.3
  15        60  '2020/12/15'     98       123     275.0
  16        60  '2020/12/16'     98       120     215.2
  17        60  '2020/12/17'    100       120     300.0
  18        60  '2020/12/19'    103       123     323.0
  19        45  '2020/12/20'     97       125     243.0
  20        60  '2020/12/21'    108       131     364.2
  21        45           NaN    100       119     282.0
  22        60  '2020/12/23'    130       101     300.0
  23        45  '2020/12/24'    105       132     246.0
  24        60  '2020/12/25'    102       126     334.5
  25        60  '2020/12/26'    100       120     250.0
  26        60  '2020/12/27'     92       118     241.0
  27        60  '2020/12/29'    100       132     280.0
  28        60  '2020/12/30'    102       129     380.3
  29        60  '2020/12/31'     92       115     243.0    

 3)使用 0 替换空字段

import pandas as pd

df = pd.read_csv('property-data.csv')
# 默认情况下,fillna() 方法返回一个新的 DataFrame,不会修改源数据。
# 如需要修改源数据,则需要使用inplace = True
df.fillna(0, inplace = True)

print(df.to_string())

输出结果:

      Duration          Date  Number  Count Calories
  0         60  '2020/12/01'    110       130     409.1
  1         60  '2020/12/02'    117       145     479.0
  2         60  '2020/12/03'    103       135     340.0
  3         45  '2020/12/04'    109       175     282.4
  4         45  '2020/12/05'    117       148     406.0
  5         60  '2020/12/06'    102       127     300.0
  6         60  '2020/12/07'    110       136     374.0
  7        450  '2020/12/08'    104       134     253.3
  8         30  '2020/12/09'    109       133     195.1
  9         60  '2020/12/10'     98       124     269.0
  10        60  '2020/12/11'    103       147     329.3
  11        60  '2020/12/12'    100       120     250.7
  12        60  '2020/12/12'    100       120     250.7
  13        60  '2020/12/13'    106       128     345.3
  14        60  '2020/12/14'    104       132     379.3
  15        60  '2020/12/15'     98       123     275.0
  16        60  '2020/12/16'     98       120     215.2
  17        60  '2020/12/17'    100       120     300.0
  18        45  '2020/12/18'     90       112       0
  19        60  '2020/12/19'    103       123     323.0
  20        45  '2020/12/20'     97       125     243.0
  21        60  '2020/12/21'    108       131     364.2
  22        45           0    100       119     282.0
  23        60  '2020/12/23'    130       101     300.0
  24        45  '2020/12/24'    105       132     246.0
  25        60  '2020/12/25'    102       126     334.5
  26        60   ‘2020/12/26’    100       120     250.0
  27        60  '2020/12/27'     92       118     241.0
  28        60  '2020/12/28'    103       132       0
  29        60  '2020/12/29'    100       132     280.0
  30        60  '2020/12/30'    102       129     380.3
  31        60  '2020/12/31'     92       115     243.0    

4、Pandas 清洗格式错误数据

有数据格式错误的单元格,可以通过将空单元格的行,或者将列中的所有单元格转换为相同格式的数据。

import pandas as pd

# 第三个日期格式错误
data = {
  "Date": ['2022/11/01', '2022/11/02' , '20221126'],
  "Count": [50, 40, 45]
}

df = pd.DataFrame(data, index = ["day1", "day2", "day3"])

df['Date'] = pd.to_datetime(df['Date'])

print(df.to_string())

5、Pandas 清洗错误数据

对于错误数据的清洗,可以尝试对错误的数据进行替换或删除。

import pandas as pd

data = {
  "name": ['Python', 'Java' , 'C'],
  "count": [1, 2, -1]    # -1的数据是有问题
}

df = pd.DataFrame(data)

for x in df.index:
  if df.loc[x, "count"] < 0:
    df.drop(x, inplace = True)

print(df.to_string())

6、Pandas 清洗重复数据

对重复数据的清洗,可以使用duplicated() 和 drop_duplicates() 。

相关文档:

Python pandas.DataFrame.duplicated函数方法的使用

Python pandas.DataFrame.drop_duplicates函数方法的使用

import pandas as pd

data = {
  "name": ['Levi', 'Levi', 'Lynn', 'John'],
  "age": [30, 30, 20, 23]  
}
df = pd.DataFrame(data)

print(df.duplicated())

df.drop_duplicates(inplace = True)
print(df)

推荐文档