Pandas 缺失数据处理的实现缺失数据处理的实现
主要介绍了Pandas 缺失数据处理的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定
的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
数据丢失(缺失)在现实生活中总是一个问题。 机器学习和数据挖掘等领域由于数据缺失导致的数据质量差,在模型预测的准确
性上面临着严重的问题。 在这些领域,缺失值处理是使模型更加准确和有效的重点。
使用重构索引(reindexing),创建了一个缺少值的DataFrame。 在输出中,NaN表示不是数字的值。
一、检查缺失值一、检查缺失值
为了更容易地检测缺失值(以及跨越不同的数组dtype),Pandas提供了isnull()和notnull()函数,它们也是Series和DataFrame对
象的方法
示例1
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)
print('')
print (df['one'].isnull())
输出结果:
one two three
a 0.036297 -0.615260 -1.341327
b NaN NaN NaN
c -1.908168 -0.779304 0.212467
d NaN NaN NaN
e 0.527409 -2.432343 0.190436
f 1.428975 -0.364970 1.084148
g NaN NaN NaN
h 0.763328 -0.818729 0.240498
a False
b True
c False
d True
e False
f False
g True
h False
Name: one, dtype: bool
示例2
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'].notnull())
输出结果:
a True
b False
c True
d False