Pandas缺失数据
数据丢失(缺失)在现实生活中总是一个问题。 机器学习和数据挖掘等领域由于数据缺失导致的数据质量差,在模型预测的准确性上面临着严重的问题。 在这些领域,缺失值处理是使模型更加准确和有效的重点。
何时以及为什么数据丢失?
想象一下有一个产品的在线调查。很多时候,人们不会分享与他们有关的所有信息。 很少有人分享他们的经验,但不是他们使用产品多久; 很少有人分享使用产品的时间,经验,但不是他们的个人联系信息。 因此,以某种方式或其他方式,总会有一部分数据总是会丢失,这是非常常见的现象。
现在来看看如何处理使用Pandas的缺失值(如NA或NaN)。
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.691764 -0.118095 -0.950871
b NaN NaN NaN
c -0.886898 0.053705 -1.269253
d NaN NaN NaN
e -0.344967 -0.837128 0.730831
f -1.193740 1.767796 0.888104
g NaN NaN NaN
h -0.755934 -1.331638 0.272248
使用重构索引(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['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将被视为0 - 如果数据全部是
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.6163354325445014
示例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()函数可以通过几种方法用非空数据“填充”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.479425 -1.711840 -1.453384
b NaN NaN NaN
c -0.733606 -0.813315 0.476788
NaN replaced with '0':
one two three
a -0.479425 -1.711840 -1.453384
b 0.000000 0.000000 0.000000
c -0.733606 -0.813315 0.476788
在这里填充零值; 当然,也可以填写任何其他的值。
填写NA前进和后退
使用重构索引章节讨论的填充概念,来填补缺失的值。
| 方法 | 动作 |
|---|---|
pad/fill |
填充方法向前 |
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.614938 -0.452498 -2.113057
b 0.614938 -0.452498 -2.113057
c -0.118390 1.333962 -0.037907
d -0.118390 1.333962 -0.037907
e 0.699733 0.502142 -0.243700
f 0.544225 -0.923116 -1.123218
g 0.544225 -0.923116 -1.123218
h -0.669783 1.187865 1.112835
示例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 2.278454 1.550483 -2.103731
b -0.779530 0.408493 1.247796
c -0.779530 0.408493 1.247796
d 0.262713 -1.073215 0.129808
e 0.262713 -1.073215 0.129808
f -0.600729 1.310515 -0.877586
g 0.395212 0.219146 -0.175024
h 0.395212 0.219146 -0.175024
丢失缺少的值
如果只想排除缺少的值,则使用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.719623 0.028103 -1.093178
c 0.040312 1.729596 0.451805
e -1.029418 1.920933 1.289485
f 1.217967 1.368064 0.527406
h 0.667855 0.147989 -1.035978
示例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
Pandas缺失数据的更多相关文章
- 利用Python进行数据分析(10) pandas基础: 处理缺失数据
数据不完整在数据分析的过程中很常见. pandas使用浮点值NaN表示浮点和非浮点数组里的缺失数据. pandas使用isnull()和notnull()函数来判断缺失情况. 对于缺失数据一般处理 ...
- Pandas汇总和处理缺失数据
汇总的函数 方法 说明 count 非NA的值数量 describe 针对Series和DataFrame列计算汇总统计 min.max 计算最小值和最大值 argmin.argmax 计算能够获取到 ...
- Pandas之Dropna滤除缺失数据
import pandas as pd import numpy as np from numpy import nan as NaN 一.处理Series对象 通过dropna()滤除缺失数据 fr ...
- pandas(五)处理缺失数据和层次化索引
pandas用浮点值Nan表示浮点和非浮点数组中的缺失数据.它只是一个便于被检测的标记而已. >>> string_data = Series(['aardvark','artich ...
- pandas知识点(处理缺失数据)
pandas使用浮点值NaN表示浮点和非浮点数组中的缺失数据: In [14]: string_data = Series(['aardvark','artichoke',np.nan,'avocad ...
- python pandas 合并数据函数merge join concat combine_first 区分
pandas对象中的数据可以通过一些内置的方法进行合并:pandas.merge,pandas.concat,实例方法join,combine_first,它们的使用对象和效果都是不同的,下面进行区分 ...
- Pandas缺失数据处理
Pandas缺失数据处理 Pandas用np.nan代表缺失数据 reindex() 可以修改 索引,会返回一个数据的副本: df1 = df.reindex(index=dates[0:4], co ...
- 基于pandas进行数据预处理
很久没用pandas,有些有点忘了,转载一个比较完整的利用pandas进行数据预处理的博文:https://blog.csdn.net/u014400239/article/details/70846 ...
- 利用Python进行数据分析_Pandas_处理缺失数据
申明:本系列文章是自己在学习<利用Python进行数据分析>这本书的过程中,为了方便后期自己巩固知识而整理. 1 读取excel数据 import pandas as pd import ...
随机推荐
- SOE不能进入断点调试
一.前言 任何程序开发,如果不能进入断点调试,是非常的痛苦的. 如果有过SOE开发经验的人都知道,SOE开发过程中调试是非常麻烦的.任何在SOE开发模板中的修改都需要重新编译工程,重新生成.soe 文 ...
- 名义人均GDP的背后,中国真实的人均GDP是1.2万美元!(中国GDP含金量较高)
来源:天涯社区 根据IMF(国际货币基金组织)在今年4月的报告,2014年份中国人均GDP为7600美元,在185个国家当中排行第78位. 然而,根据楼主在国外行走多年的经验,巴西.墨西哥.马来西亚. ...
- Java HashMap工作原理及实现(转载)
https://yikun.github.io/2015/04/01/Java-HashMap工作原理及实现/
- 流畅的python 使用一等函数实现设计模式
案例分析:重构“策略”模式 经典的“策略”模式 电商领域有个功能明显可以使用“策略”模式,即根据客户的属性或订单中的商品计算折扣.假如一个网店制定了下述折扣规则. 有 1000 或以上积分的顾客,每个 ...
- mysql 如何找出两张表之间的关系
分析步骤: #1.先站在左表的角度去找 是否左表的多条记录可以对应右表的一条记录,如果是,则证明左表的一个字段foreign key 右表一个字段(通常是id) #2.再站在右表的角度去找 是否右表的 ...
- java反射基础知识(五)反射应用实践
详解Java反射各种应用 Java除了给我们提供在编译期得到类的各种信息之外,还通过反射让我们可以在运行期间得到类的各种信息.通过反射获取类的信息,得到类的信息之后,就可以获取以下相关内容: Cl ...
- 用pytesseract识别验证码报错
运行py文件出现下面报错 pytesseract.pytesseract.TesseractError: (1, 'Error opening data file \\Program Files\\T ...
- 谷歌Project Fi服务(转)
问:谷歌推出的移动虚拟网络Project Fi到底是什么呀? 答:谷歌手里有很多张“牌”可以出,现在这家搜索巨头又将目标放在了无线产业.在美国,移动电信合约服务是AT&T, Verizon, ...
- 数据结构&算法(一)_堆、栈(堆栈)、队列、链表
堆: ①堆通常是一个可以被看做一棵树的数组对象.堆总是满足下列性质: ·堆中某个节点的值总是不大于或不小于其父节点的值: ·堆总是一棵完全二叉树.将根节点最大的堆叫做最大堆或大根堆,根节点最小的堆叫做 ...
- ceph存储安装配置
1.修改yum源: 1.安装yum源:sudo yum install -y yum-utils sudo yum-config-manager --add-repo https://dl.fedor ...