Python - 数据清洗



在现实生活场景中,缺失数据始终是一个问题。机器学习和数据挖掘等领域由于数据质量差(由缺失值造成)导致模型预测准确性出现严重问题。在这些领域,缺失值处理是提高模型准确性和有效性的一个主要关注点。

数据何时以及为何缺失?

让我们考虑一个产品的在线调查。很多时候,人们不会分享与他们相关的所有信息。一些人分享他们的体验,但没有分享他们使用产品的时长;一些人分享他们使用产品的时长、他们的体验,但没有分享他们的联系方式。因此,数据的一部分总是以某种方式缺失,这在实时数据中非常常见。

现在让我们看看如何使用 Pandas 处理缺失值(例如 NA 或 NaN)。

# import the pandas library
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f',
'h'],columns=['one', 'two', 'three'])

df = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])

print df

输出如下所示:

         one        two      three
a   0.077988   0.476149   0.965836
b        NaN        NaN        NaN
c  -0.390208  -0.551605  -2.301950
d        NaN        NaN        NaN
e  -2.000303  -0.788201   1.510072
f  -0.930230  -0.670473   1.146615
g        NaN        NaN        NaN
h   0.085100   0.532791   0.887415

使用重新索引,我们创建了一个包含缺失值的 DataFrame。在输出中,NaN 表示非数字

检查缺失值

为了更容易检测缺失值(并在不同的数组数据类型中),Pandas 提供了isnull()notnull() 函数,它们也是 Series 和 DataFrame 对象上的方法:

示例

import pandas as pd
import numpy as np
 
df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f',
'h'],columns=['one', 'two', 'three'])

df = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])

print df['one'].isnull()

输出如下所示:

a  False
b  True
c  False
d  True
e  False
f  False
g  True
h  False
Name: one, dtype: bool

清理/填充缺失数据

Pandas 提供了多种清理缺失值的方法。fillna 函数可以通过几种方式“填充” NA 值为非空数据,我们在以下部分进行了说明。

用标量值替换 NaN

以下程序展示了如何用“0”替换“NaN”。

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(3, 3), index=['a', 'c', 'e'],columns=['one',
'two', 'three'])
df = df.reindex(['a', 'b', 'c'])
print df
print ("NaN replaced with '0':")
print df.fillna(0)

输出如下所示:

         one        two     three
a  -0.576991  -0.741695  0.553172
b        NaN        NaN       NaN
c   0.744328  -1.735166  1.749580

NaN replaced with '0':
         one        two     three
a  -0.576991  -0.741695  0.553172
b   0.000000   0.000000  0.000000
c   0.744328  -1.735166  1.749580

这里,我们用值零填充;我们也可以用任何其他值填充。

向前和向后填充 NA

使用在重新索引章节中讨论的填充概念,我们将填充缺失值。

方法 操作
pad/fill 向前填充方法
bfill/backfill 向后填充方法

示例

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f',
'h'],columns=['one', 'two', 'three'])
df = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])

print df.fillna(method='pad')

输出如下所示:

         one        two      three
a   0.077988   0.476149   0.965836
b   0.077988   0.476149   0.965836
c  -0.390208  -0.551605  -2.301950
d  -0.390208  -0.551605  -2.301950
e  -2.000303  -0.788201   1.510072
f  -0.930230  -0.670473   1.146615
g  -0.930230  -0.670473   1.146615
h   0.085100   0.532791   0.887415

删除缺失值

如果您只想排除缺失值,则可以使用dropna 函数以及axis 参数。默认情况下,axis=0,即沿行,这意味着如果一行中的任何值为 NA,则整行都会被排除。

示例

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f',
'h'],columns=['one', 'two', 'three'])

df = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])
print df.dropna()

输出如下所示:

         one        two      three
a   0.077988   0.476149   0.965836
c  -0.390208  -0.551605  -2.301950
e  -2.000303  -0.788201   1.510072
f  -0.930230  -0.670473   1.146615
h   0.085100   0.532791   0.887415

替换缺失值(或)通用值

很多时候,我们必须用一些特定值替换通用值。我们可以通过应用替换方法来实现。

用标量值替换 NA 等同于fillna() 函数的行为。

示例

import pandas as pd
import numpy as np
df = pd.DataFrame({'one':[10,20,30,40,50,2000],
'two':[1000,0,30,40,50,60]})
print df.replace({1000:10,2000:60})

输出如下所示:

   one  two
0   10   10
1   20    0
2   30   30
3   40   40
4   50   50
5   60   60
广告

© . All rights reserved.