Python - 数据清理

Python - 数据清理 首页 / 数据科学入门教程 / 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

其输出如下-

链接:https://www.learnfk.comhttps://www.learnfk.com/python-data-science/python-data-cleansing.html

来源:LearnFk无涯教程网

         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 表示不是数字。

检查缺失值

为了使检测缺失值更容易(并跨不同的数组dtypes),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()

其输出如下-

链接:https://www.learnfk.comhttps://www.learnfk.com/python-data-science/python-data-cleansing.html

来源:LearnFk无涯教程网

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

清理/填充数据

Pandas提供了多种清除缺失值的方法, fillna函数可以通过以下几种方法用非空数据"fill" NA值。

用值替换NaN

以下程序显示了如何将" NaN"替换为" 0"。

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)

其输出如下-

链接:https://www.learnfk.comhttps://www.learnfk.com/python-data-science/python-data-cleansing.html

来源:LearnFk无涯教程网

         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/fillForward
bfill/backfillBackward
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')

其输出如下-

链接:https://www.learnfk.comhttps://www.learnfk.com/python-data-science/python-data-cleansing.html

来源:LearnFk无涯教程网

         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()

其输出如下-

链接:https://www.learnfk.comhttps://www.learnfk.com/python-data-science/python-data-cleansing.html

来源:LearnFk无涯教程网

         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

替换通用值

很多时候,必须用某个特定值替换一个通用值,无涯教程可以通过应用replace方法来实现。

用标量值替换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})

其输出如下-

链接:https://www.learnfk.comhttps://www.learnfk.com/python-data-science/python-data-cleansing.html

来源:LearnFk无涯教程网

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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Go语言核心36讲 -〔郝林〕

Nginx核心知识150讲 -〔陶辉〕

玩转Spring全家桶 -〔丁雪丰〕

iOS开发高手课 -〔戴铭〕

数据中台实战课 -〔郭忆〕

用户体验设计实战课 -〔相辉〕

成为AI产品经理 -〔刘海丰〕

遗留系统现代化实战 -〔姚琪琳〕

超级访谈:对话毕玄 -〔毕玄〕

好记忆不如烂笔头。留下您的足迹吧 :)