Pandas 缺失数据处理的实现
作者:PythonGirl 发布时间:2023-07-14 05:57:38
数据丢失(缺失)在现实生活中总是一个问题。 机器学习和数据挖掘等领域由于数据缺失导致的数据质量差,在模型预测的准确性上面临着严重的问题。 在这些领域,缺失值处理是使模型更加准确和有效的重点。
使用重构索引(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('\n')
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
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)
print('\n')
print (df['one'].sum())
输出结果:
one two three
a -1.191036 0.945107 -0.806292
b NaN NaN NaN
c 0.127794 -1.812588 -0.466076
d NaN NaN NaN
e 2.358568 0.559081 1.486490
f -0.242589 0.574916 -0.831853
g NaN NaN NaN
h -0.328030 1.815404 -1.706736
0.7247067964060545
示例2
import pandas as pd
df = pd.DataFrame(index=[0,1,2,3,4,5],columns=['one','two'])
print(df)
print('\n')
print (df['one'].sum())
输出结果:
one two
0 NaN NaN
1 NaN NaN
2 NaN NaN
3 NaN NaN
4 NaN NaN
5 NaN NaN0
三、填充缺少数据
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('\n')
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.476788NaN 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是fillna()函数的等效行为。
示例
import pandas as pd
df = pd.DataFrame({'one':[10,20,30,40,50,2000],'two':[1000,0,30,40,50,60]})
print(df)
print('\n')
print (df.replace({1000:10,2000:60}))
输出结果:
one two
0 10 1000
1 20 0
2 30 30
3 40 40
4 50 50
5 2000 60one two
0 10 10
1 20 0
2 30 30
3 40 40
4 50 50
5 60 60
填写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)
print('\n')
print (df.fillna(method='pad'))
输出结果:
one two three
a -0.023243 1.671621 -1.687063
b NaN NaN NaN
c -0.933355 0.609602 -0.620189
d NaN NaN NaN
e 0.151455 -1.324563 -0.598897
f 0.605670 -0.924828 -1.050643
g NaN NaN NaN
h 0.892414 -0.137194 -1.101791
one two three
a -0.023243 1.671621 -1.687063
b -0.023243 1.671621 -1.687063
c -0.933355 0.609602 -0.620189
d -0.933355 0.609602 -0.620189
e 0.151455 -1.324563 -0.598897
f 0.605670 -0.924828 -1.050643
g 0.605670 -0.924828 -1.050643
h 0.892414 -0.137194 -1.101791
示例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]
来源:https://www.cnblogs.com/Summer-skr--blog/p/11705887.html
猜你喜欢
- 在广大网友心目中,他们就是中国互联网搜索领域的三驾马车。无论这三家搜索巨头承不承认,在网友眼中总会来将他们进行对比比较。当然,更多时候的比较
- 今天网页调试的时候在线订单出现错误:Server 对象 错误 'ASP 0178
- 本文实例讲述了php指定长度分割字符串str_split函数用法。分享给大家供大家参考,具体如下:示例1:$str = 'abcde
- BLOG地址:http://www.planabc.net/article.asp?id=107学习标准的朋友,一般都会在学习的过程中接触到
- 一、临时表空间概念临时表空间用来管理数据库排序操作以及用于存储临时表、中间排序结果等临时对象,当ORACLE里需要用到SORT的时候,并且当
- SQL Server管理,你做得好吗?下文这些可能会对你的工作有一些帮助,不妨耐心的看看吧。1. 数据库文件有.mdf .ndf .ldf三
- 分区表的概念 分区致力于解决支持极大表和索引的关键问题。它采用他们分解成较小和易于管理的称为分区的片(piece)的方法。一旦分区被定义,S
- 在印刷排版中“point”是一个绝对的单位,它等于 1/72 英寸。可以用尺子丈量的,物理的英寸。但在CSS中pt的含义
- FlashPaper 是Macromedia推出的一款电子文档类工具,通过使用本程序,你可以将需要的文档通过简单的设置转换为SWF格式的Fl
- 我正在开发一个档案管理系统,需要从数据库中同时调出图像及相关的文字说明,可我只做到了单纯地显示图片,像有一个数据库CHUNFENG,在数据库
- MySQL中有很多的基本命令,show命令也是其中之一,在很多使用者中对show命令的使用还容易产生混淆,本文主要介绍了show命令的主要用
- 一个XML文档如果符合一些基本的规范,那它就是结构规范的。XML格式有一套比HTML简单的解析规则,允许XML解析器不需要外部描述或了解数据
- Firefox 3 有一个很让人讨厌的bug:基于某种目的,在表单提交时 disable 掉提交按钮,通过后退键回到这个页面后,这个提交按钮
- 一. 建库,建表,加约束. 1.1建库 代码如下:use master go if exists (select * from sysdat
- 本文实例讲述了Go语言通过Luhn算法验证信用卡卡号是否有效的方法。分享给大家供大家参考。具体实现方法如下:package mainimpo
- 一般情况下TextArea区输入的文字数量是没有限制的,但是我们可以通过javascript限制表单的文字字数。如下javascript代码
- 代码如下: <% Dim oConn, ors, aRows Dim i,j Set oConn=Server.CreateObjec
- Mysql数据库常用命令:启动Mysql数据库C:》cd Mysql5.0 binC:Mysql5.0 bin》mysqld –instal
- <% dim conn,mdbfile mdbfile=server.mappath("数据库名称.mdb") s
- XML即可扩展标记语言(eXtensible Markup Language)。标记是指计算机所能理解的信息符号,通过此种标记,计算机之间可