Python Pandas 数据缺失 您所在的位置:网站首页 isoweekday什么意思 Python Pandas 数据缺失

Python Pandas 数据缺失

#Python Pandas 数据缺失| 来源: 网络整理| 查看: 265

Python Pandas 数据缺失

在现实生活中,缺失数据始终是一个问题。像机器学习和数据挖掘这样的领域,由于缺失值造成的数据质量差,他们的模型预测的准确性面临严重问题。在这些领域,缺失值的处理是一个主要的焦点,以使他们的模型更加准确和有效。

什么时候以及为什么会出现数据缺失

让我们考虑一个产品的在线调查。很多时候,人们并不分享与他们相关的所有信息。少数人分享他们的经验,但不分享他们使用产品的时间;少数人分享他们使用产品的时间,他们的经验,但不分享他们的联系信息。因此,在某种程度上,总有一部分数据缺失,这种情况在实时中非常普遍。

现在让我们看看如何使用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() 函数,这也是系列和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['one'].isnull()

其 输出 情况如下—

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 e True f True g False h True Name: one, dtype: bool 有缺失数据的计算方法 当对数据进行求和时,NA将被视为零。 如果数据都是NA,那么结果将是NA。 例子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['one'].sum()

其 输出结果 如下 –

2.02357685917 例子2 import pandas as pd import numpy as np df = pd.DataFrame(index=[0,1,2,3,4,5],columns=['one','two']) print df['one'].sum()

其 输出结果 如下 –

nan 清理/填补缺失的数据

Pandas提供了各种清理缺失值的方法。fillna函数可以用非空数据 “填充 “缺失值,有几种方法,我们在下面的章节中进行了说明。

用一个标量值替换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

利用ReIndexing章节中讨论的填充概念,我们将填充缺失的值。

序号 方法和行动 1 pad/fill 填充方法 前进 2 bfill/backfill 填充法 向后 示例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.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 例子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.fillna(method='backfill')

其 输出结果 如下 –

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

如果你想简单地排除缺失值,那么请使用 dropna 函数和 axis 参数。默认情况下,axis=0,即沿行,这意味着如果某行内的任何数值是NA,那么整个行都会被排除。

例子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.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 例子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.dropna(axis=1)

其 输出结果 如下 –

Empty DataFrame Columns: [ ] Index: [a, b, c, d, e, f, g, h] 替换缺失的(或)通用值

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

用一个标量值替换NA是 fillna() 函数的等效行为。

例子1 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 例子2 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


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有