pandas fillna详解

pandas fillna详解pandas中补全nan具体的参数Series.fillna(self,value=None,method=None,axis=None,inplace=False,limit=None,downcast=None,**kwargs)[source]参数: value:scalar,dict,Series,orDataFrameValuetouset…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

pandas中补全nan


具体的参数
Series.fillna(self, value=None, method=None, axis=None, inplace=False, limit=None, downcast=None, **kwargs)[source]


参数:	
value : scalar, dict, Series, or DataFrame
Value to use to fill holes (e.g. 0), alternately a dict/Series/DataFrame of values specifying which value to use for each index (for a Series) or column (for a DataFrame). Values not in the dict/Series/DataFrame will not be filled. This value cannot be a list.

其他的参数:

method : { 
   ‘backfill’, ‘bfill’, ‘pad’, ‘ffill’, None}, default None
Method to use for filling holes in reindexed Series pad / ffill: propagate last valid observation forward to next valid backfill / bfill: use next valid observation to fill gap.

axis : { 
   0 or ‘index’}
Axis along which to fill missing values.

inplace : bool, default False
If True, fill in-place. Note: this will modify any other views on this object (e.g., a no-copy slice for a column in a DataFrame).

limit : int, default None
If method is specified, this is the maximum number of consecutive NaN values to forward/backward fill. In other words, if there is a gap with more than this number of consecutive NaNs, it will only be partially filled. If method is not specified, this is the maximum number of entries along the entire axis where NaNs will be filled. Must be greater than 0 if not None.

downcast : dict, default is None
A dict of item->dtype of what to downcast if possible, or the string ‘infer’ which will try to downcast to an appropriate equal type (e.g. float64 to int64 if possible).

Returns:	
Series
Object with missing values filled.

例子:

>>> df = pd.DataFrame([[np.nan, 2, np.nan, 0],
...                    [3, 4, np.nan, 1],
...                    [np.nan, np.nan, np.nan, 5],
...                    [np.nan, 3, np.nan, 4]],
...                   columns=list('ABCD'))
>>> df
     A    B   C  D
0  NaN  2.0 NaN  0
1  3.0  4.0 NaN  1
2  NaN  NaN NaN  5
3  NaN  3.0 NaN  4

补零

>>> df.fillna(0)
    A   B   C   D
0   0.0 2.0 0.0 0
1   3.0 4.0 0.0 1
2   0.0 0.0 0.0 5
3   0.0 3.0 0.0 4

向前补充,按列 ffill forward fill

>>> df.fillna(method='ffill')
    A   B   C   D
0   NaN 2.0 NaN 0
1   3.0 4.0 NaN 1
2   3.0 4.0 NaN 5
3   3.0 3.0 NaN 4

改变方向 axis = 1按行的方向

>>> df.fillna(method='ffill',axis=1)
 	A	B	C	D
0	NaN	2.0	2.0	0.0
1	3.0	4.0	4.0	1.0
2	NaN	NaN	NaN	5.0
3	NaN	3.0	3.0	4.0

按字典补充,列名:value

>>> values = { 
   'A': 0, 'B': 1, 'C': 2, 'D': 3}
>>> df.fillna(value=values)
    A   B   C   D
0   0.0 2.0 2.0 0
1   3.0 4.0 2.0 1
2   0.0 1.0 2.0 5
3   0.0 3.0 2.0 4

用limit限制补充的个数

>>> df.fillna(value=values, limit=1)
    A   B   C   D
0   0.0 2.0 2.0 0
1   3.0 4.0 NaN 1
2   NaN 1.0 NaN 5
3   NaN 3.0 NaN 4

实际中常用的按均值补充。

for column in list(df.columns[df.isnull().sum() > 0]):
    mean_val = df[column].mean()
    df[column].fillna(mean_val, inplace=True)

这是用来查看需要补充的列

list(df.columns[df.isnull().sum() > 0])
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/170010.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • JMH 性能测试分析工具

    JMH 性能测试分析工具一什么是JMHJMH是在method层面上的benchmark,精度可以精确到微秒级,是对热点函数进行优化时,对优化结果进行定量分析的工具。二JMH的应用场景典型场景:想定量地知道某个函数需要执行多长时间,以及执行时间和输入n的相关性。 一个函数有多种不同的实现,针对多种不同的实现,需要定量分析出那种实现性能更好。三JMH的使用3.1引入依赖<properties><jmh.version>1.14.1&…

    2022年7月11日
    22
  • ssm框架过时了吗_spring实战

    ssm框架过时了吗_spring实战SpringSpring是一个开源的免费的框架Spring是一个轻量级的,非入侵式的框架控制反转(IOC),面向切面编程(AOP)支持事务的处理,对框架整合的支持IOC理论UserDaoUserDaoImpUserSeviceUserServiceImp在之前,用户的需求可能会影响原来的代码。使用一个set。public void setUserDao(UserDao userDao){ this.userDao = userDao;}之前是主动创建对象,控制

    2022年8月9日
    1
  • c语言如何遍历数组,C语言数组遍历

    c语言如何遍历数组,C语言数组遍历C语言数组遍历教程C语言for循环遍历数组详解语法for(i=0;i<count;i++){//arr[i]}说明其中count是数组的元素的个数,此时,数组的每一个元素是arr[i]。C语言while循环遍历数组详解语法inti=0;while(i<count){//arr[i]i++;}说明其中count是数组的元素的个数,此时,数组的每一个元…

    2022年7月22日
    7
  • Arping命令手册

    Arping命令手册Arping命令手册  arping-sendARPREQUESTtoaneighbourhost注释:arping是用于发送ARP请求到一个相邻主机的工具SYNOPSIS  arping  [  -AbDfhqUV][  -ccount]

    2022年6月3日
    30
  • 解决IDEA插件安装慢、超时、不成功问题[通俗易懂]

    解决IDEA插件安装慢、超时、不成功问题[通俗易懂]解决IDEA插件安装慢、超时、不成功问题1.修改本地hosts文件,打开文件位置:Windows系统Hosts文件路径:C:\Windows\System32\drivers\etc\hosts用工具打开hosts文件2.打开国内插件的节点IP地址http://tool.chinaz.com/speedtest/plugins.jetbrains.com在检测结果中选择一个相对耗时少的IP地址,因为比较快然后按照第一步在hosts文件里加上即可,然后保存(需要以管理员身份)3.重

    2022年5月11日
    171
  • js 数组splice_splice会影响原数组吗

    js 数组splice_splice会影响原数组吗今天用到了数组的删除,分别使用了splice和delete方法,记录一下,方便下次查找。原数组是一个关联数组,如vartest=[];test[1]={name:’1′,age:1};test[2]={name:’2′,age:2};test[4]={name:’3′,age:3};console.log(test)长度为5的关联数组,现在开始删除。1.splice方法test.splice(2

    2022年10月1日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号