Pandas之Fillna填充缺失数据的方法
作者:yungeisme 时间:2022-11-17 20:17:12
约定:
import pandas as pd
import numpy as np
from numpy import nan as NaN
填充缺失数据
fillna()是最主要的处理方式了。
df1=pd.DataFrame([[1,2,3],[NaN,NaN,2],[NaN,NaN,NaN],[8,8,NaN]])
df1
代码结果:
0 | 1 | 2 | |
---|---|---|---|
0 | 1.0 | 2.0 | 3.0 |
1 | NaN | NaN | 2.0 |
2 | NaN | NaN | NaN |
3 | 8.0 | 8.0 | NaN |
用常数填充:
df1.fillna(100)
代码结果:
0 | 1 | 2 | |
---|---|---|---|
0 | 1.0 | 2.0 | 3.0 |
1 | 100.0 | 100.0 | 2.0 |
2 | 100.0 | 100.0 | 100.0 |
3 | 8.0 | 8.0 | 100.0 |
通过字典填充不同的常数:
df1.fillna({0:10,1:20,2:30})
代码结果:
0 | 1 | 2 | |
---|---|---|---|
0 | 1.0 | 2.0 | 3.0 |
1 | 10.0 | 20.0 | 2.0 |
2 | 10.0 | 20.0 | 30.0 |
3 | 8.0 | 8.0 | 30.0 |
传入inplace=True直接修改原对象:
df1.fillna(0,inplace=True)
df1
代码结果:
0 | 1 | 2 | |
---|---|---|---|
0 | 1.0 | 2.0 | 3.0 |
1 | 0.0 | 0.0 | 2.0 |
2 | 0.0 | 0.0 | 0.0 |
3 | 8.0 | 8.0 | 0.0 |
传入method=” “改变插值方式:
df2=pd.DataFrame(np.random.randint(0,10,(5,5)))
df2.iloc[1:4,3]=NaN;df2.iloc[2:4,4]=NaN
df2
代码结果:
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 6 | 6 | 2 | 4.0 | 1.0 |
1 | 4 | 7 | 0 | NaN | 5.0 |
2 | 6 | 5 | 5 | NaN | NaN |
3 | 1 | 9 | 9 | NaN | NaN |
4 | 4 | 8 | 1 | 5.0 | 9.0 |
df2.fillna(method='ffill')#用前面的值来填充
代码结果:
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 6 | 6 | 2 | 4.0 | 1.0 |
1 | 4 | 7 | 0 | 4.0 | 5.0 |
2 | 6 | 5 | 5 | 4.0 | 5.0 |
3 | 1 | 9 | 9 | 4.0 | 5.0 |
4 | 4 | 8 | 1 | 5.0 | 9.0 |
传入limit=” “限制填充个数:
df2.fillna(method='bfill',limit=2)
代码结果:
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 6 | 6 | 2 | 4.0 | 1.0 |
1 | 4 | 7 | 0 | NaN | 5.0 |
2 | 6 | 5 | 5 | 5.0 | 9.0 |
3 | 1 | 9 | 9 | 5.0 | 9.0 |
4 | 4 | 8 | 1 | 5.0 | 9.0 |
传入axis=” “修改填充方向:
df2.fillna(method="ffill",limit=1,axis=1)
代码结果:
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 6.0 | 6.0 | 2.0 | 4.0 | 1.0 |
1 | 4.0 | 7.0 | 0.0 | 0.0 | 5.0 |
2 | 6.0 | 5.0 | 5.0 | 5.0 | NaN |
3 | 1.0 | 9.0 | 9.0 | 9.0 | NaN |
4 | 4.0 | 8.0 | 1.0 | 5.0 | 9.0 |
来源:https://blog.csdn.net/weixin_38168620/article/details/79596819
标签:Pandas,Fillna,填充,缺失数据
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Pyinstaller 打包exe教程及问题解决
2023-01-08 01:40:00
Go逃逸分析示例详解
2024-04-25 15:05:22
![](https://img.aspxhome.com/file/2023/2/134232_0s.jpg)
Python自动化测试Eclipse+Pydev 搭建开发环境
2022-07-15 07:01:54
![](https://img.aspxhome.com/file/2023/1/107011_0s.png)
在python里使用await关键字来等另外一个协程的实例
2021-03-01 19:10:16
Python 图片转数组,二进制互转操作
2023-10-07 17:46:04
![](https://img.aspxhome.com/file/2023/4/89064_0s.png)
Oracle数据库opatch补丁操作流程
2024-01-28 06:51:42
![](https://img.aspxhome.com/file/2023/9/64849_0s.png)
利用机器学习预测房价
2023-08-10 21:25:50
![](https://img.aspxhome.com/file/2023/8/64608_0s.png)
设计地址栏透明icon图标方法
2008-10-25 16:42:00
![](https://img.aspxhome.com/file/UploadPic/200810/25/20081025172534705s.jpg)
Python实现一个简单三层神经网络的搭建及测试 代码解析
2021-10-09 23:59:20
![](https://img.aspxhome.com/file/2023/7/134637_0s.png)
CSS教程:关于网页图片的属性的介绍
2008-10-31 12:02:00
Mysql中正则表达式Regexp常见用法及说明
2024-01-14 21:51:35
MySQL的root密码丢失解决方法
2011-05-05 15:56:00
pytorch交叉熵损失函数的weight参数的使用
2021-02-27 15:52:31
![](https://img.aspxhome.com/file/2023/3/101703_0s.jpg)
AJAX的jQuery实现入门(二)
2008-05-01 13:04:00
Windows下MySQL 5.7无法启动的解决方法
2024-01-26 02:32:31
![](https://img.aspxhome.com/file/2023/8/75648_0s.png)
浅谈为什么#{}可以防止SQL注入
2024-01-27 07:19:08
完美解决torch.cuda.is_available()一直返回False的玄学方法
2023-04-05 18:17:34
![](https://img.aspxhome.com/file/2023/8/128218_0s.jpg)
linux下利用Docker安装mysql的步骤
2024-01-25 01:17:56
ASP.NET 2.0防止同一用户同时登录
2007-10-03 14:30:00
python 实现 pymysql 数据库操作方法
2024-01-22 09:56:29