1、检查缺失值

为了更容易地检测缺失值(以及跨越不同的数组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())
Python
执行上面示例代码,得到以下结果 - a False
b True
c False
d True
e False
f False
g True
h False
Name: one, dtype: bool
Shell
示例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())
Python
执行上面示例代码,得到以下结果 - a True
b False
c True
d False
e True
f True
g False
h True
Name: one, dtype: bool
Shell
缺少数据的计算
在求和数据时,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())
Python
执行上面示例代码,得到以下结果 - -2.6163354325445014
Shell
示例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())
Python
执行上面示例代码,得到以下结果 - nan

2、清理/填充缺少

数据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))
Python
执行上面示例代码,得到以下结果 - one two three
a -0.479425 -1.711840 -1.453384
b NaN NaN NaN
c -0.733606 -0.813315 0.476788
NaN replaced with '':
one two three
a -0.479425 -1.711840 -1.453384
b 0.000000 0.000000 0.000000
c -0.733606 -0.813315 0.476788
Shell
在这里填充零值; 当然,也可以填写任何其他的值。 填写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'))
Python
执行上面示例代码,得到以下结果 - 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
Shell
示例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'))
Python
执行上面示例代码,得到以下结果 - 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

3、丢失缺少的值

如果只想排除缺少的值,则使用dropna函数和axis参数。 默认情况下,axis = 0,即在行上应用,这意味着如果行内的任何值是NA,那么整个行被排除。

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())
Python
执行上面示例代码,得到以下结果 - 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
Shell
示例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))
Python
执行上面示例代码,得到以下结果 - Empty DataFrame
Columns: []
Index: [a, b, c, d, e, f, g, h]
Shell
替换丢失(或)通用值
很多时候,必须用一些具体的值取代一个通用的值。可以通过应用替换方法来实现这一点。 用标量值替换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}))
Python
执行上面示例,得到以下结果 - one two
0 10 10
1 20 0
2 30 30
3 40 40
4 50 50
5 60 60

pandas缺失值处理的更多相关文章

  1. Python数据分析(二)pandas缺失值处理

    import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e' ...

  2. Python—关于Pandas缺失值问题(国内唯一)

    获取文中的CSV文件用于代码编程以及文章首发地址,请点击下方超链接 获取CSV,用于编程调试请点这 在本文中,我们将使用Python的Pandas库逐步完成许多不同的数据清理任务.具体而言,我们将重点 ...

  3. Pandas系列(六)-时间序列详解

    内容目录 1. 基础概述 2. 转换时间戳 3. 生成时间戳范围 4. DatetimeIndex 5. DateOffset对象 6. 与时间序列相关的方法 6.1 移动 6.2 频率转换 6.3 ...

  4. Pandas 时间序列

    # 导入相关库 import numpy as np import pandas as pd 在做金融领域方面的分析时,经常会对时间进行一系列的处理.Pandas 内部自带了很多关于时间序列相关的工具 ...

  5. Python 基础(五)

    pandas缺失值处理 import pandas as pd importrandom df01 = pd.DataFrame(np.random.randint(1,9),size = (4,4) ...

  6. Pandas系列(三)-缺失值处理

    内容目录 1. 什么是缺失值 2. 丢弃缺失值 3. 填充缺失值 4. 替换缺失值 5. 使用其他对象填充 数据准备 import pandas as pd import numpy as np in ...

  7. 【学习】数据处理基础知识(缺失值处理)【pandas】

    缺失数据(missing data)大部分数据分析应用中非常常见.pd设计目标之一就是让缺失数据的处理任务尽量轻松. pd 使用浮点值NaN(Not a Number) 表示浮点和非浮点数组中的缺失数 ...

  8. Python Pandas找到缺失值的位置

    python pandas判断缺失值一般采用 isnull(),然而生成的却是所有数据的true/false矩阵,对于庞大的数据dataframe,很难一眼看出来哪个数据缺失,一共有多少个缺失数据,缺 ...

  9. pandas判断缺失值的办法

    参考这篇文章: https://blog.csdn.net/u012387178/article/details/52571725 python pandas判断缺失值一般采用 isnull(),然而 ...

随机推荐

  1. C++多线程中用临界区控制全局变量的访问冲突问题

    困扰了我很长时间的多线程访问全局变量今天终于解决了,所以得记录一下..控制全局变量的方法很多,有信号量.临界区等..这里我记录一个用临界区控制访问冲突的例子.非常好用. #include <wi ...

  2. ztree树形菜单demo

    阅读目录 zTree树形菜单 回到顶部 zTree树形菜单 树形菜单使用方式如下:HTML引入的方式如下: <!DOCTYPE html> <html> <head> ...

  3. PAT A1140 Look-and-say Sequence (20 分)——数学题

    Look-and-say sequence is a sequence of integers as the following: D, D1, D111, D113, D11231, D112213 ...

  4. PAT A1148 Werewolf - Simple Version (20 分)——暴力遍历,负负得正

    Werewolf(狼人杀) is a game in which the players are partitioned into two parties: the werewolves and th ...

  5. JSON构造/解析(by C)---cJSON和json-c

    背景 JSON即JavaScript Object Notation,是一种轻量级的数据交换格式. JSON建构于两种结构: "名称/值"对的集合(A collection of ...

  6. maven 插

    一.maven插件元素 <?xml version="1.0" encoding="utf-8"?> <plugin> <!--插 ...

  7. openssl生成签名与验证签名

    继上一篇RSA对传输信息进行加密解密,再写个生成签名和验证签名. 一般,安全考虑,比如接入支付平台时,请求方和接收方要互相验证是否是你,就用签名来看. 签名方式一般两种,对称加密和非对称加密.对称加密 ...

  8. git使用备注

    git clone 代码库地址 git branch -r  查看远程分支 git branch 查看本地分支 git branch -a 查看远程和本地分支.带*的表示正在所处分支. git bra ...

  9. Luogu4199 万径人踪灭 FFT、Manacher

    传送门 先不考虑”不是连续的一段“这一个约束条件.可以知道:第$i$位与第$j$位相同,可以对第$\frac{i+j}{2}$位置上产生$1$的贡献(如果$i+j$为奇数表明它会对一条缝产生$1$的贡 ...

  10. 【APIO2016】烟火表演

    题面 题解 神仙题目啊QwQ 设\(f_i(x)\)表示以第\(i\)个点为根的子树需要\(x\)秒引爆的代价. 我们发现,这个函数是一个下凸的一次分段函数. 考虑这个函数合并到父亲节点时会发生怎样的 ...