数据丢失在现实生活中是一个问题。 机器学习和数据挖掘等领域由于数据缺失导致数据质量差,因此在模型预测的准确性方面面临严峻的问题。 在这些领域,缺失值处理是使模型更加准确和有效的关键。
# import the pandas libraryimport pandas as pdimport numpy as npdf = 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)Python
它将输出如下结果 -
one two threea 0.077988 0.476149 0.965836b NaN NaN NaNc -0.390208 -0.551605 -2.301950d NaN NaN NaNe -2.000303 -0.788201 1.510072f -0.930230 -0.670473 1.146615g NaN NaN NaNh 0.085100 0.532791 0.887415Shell
使用reindexing,创建了一个缺失值的DataFrame。 在输出中,NaN表示不是数字。
检查缺失值
为了更容易地检测缺失值(以及跨越不同的数组dtype),Pandas提供了isnull()和notnull()函数,它们也是Series