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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • C语言 最长回文子串[通俗易懂]

    C语言 最长回文子串[通俗易懂]描述:输入一个字符串,求其中最长回文子串。子串的含义是:在字符串中连续出现得字符串片段。回文的含义是,正着看和倒着看是相同的,如abba何abbebba。在判断时要求忽略所有的标点和空格,且忽略大小写。但输出时按原样输出(首尾不要输出多余的字符串).输入字符串长度大于等于1小于等于5000.且单独占一行。输入:输入一行字符串。输出:输出所要求的回文子串。样例输入:L…

    2022年6月7日
    50
  • 耳机线的接法_耳机线焊接法图解大全

    耳机线的接法_耳机线焊接法图解大全耳机是我们日常必备工具,尤其是在公共场所或是夜深人静时!之前一直用的是入耳式的耳机,隔音效果好,声音大,所以下狠心买了一个用过最贵的耳机90+元。但是买回用了不到两个星期就让我给弄报废了,而且入耳式的耳机戴久了耳朵塞的疼。所以我就淘宝买了个8.6元包邮的山寨苹果耳机,结果带给了我很多惊喜,没想到音质丝毫不比之前的任何耳机差,戴着也舒服。最重要的是隔音效果好,每当夜深人静时无论是听音乐还是看剧都与世…

    2025年7月14日
    0
  • Java编程的逻辑 (4) – 整数的二进制表示与位运算

    Java编程的逻辑 (4) – 整数的二进制表示与位运算

    2021年9月14日
    39
  • U8 8.9 数据库置疑恢复方法

    U8 8.9 数据库置疑恢复方法A.我们使用默认方式建立一个供恢复使用的数据库(如test)。可以在SQLServerEnterpriseManager里面建立。B.停掉数据库服务器。C.将刚才生成的数据库的日志文件test_log.ldf删除,用要恢复的数据库mdf文件覆盖刚才生成的数据库数据文件test_data.mdf。D.启动数据库服务器。此时会看到数据库test的状态为“置疑”。这时候不能对此数据库进行任何操作。…

    2022年8月20日
    3
  • 为啥国人偏爱Mybatis,而老外喜欢Hibernate/JPA呢?

    为啥国人偏爱Mybatis,而老外喜欢Hibernate/JPA呢?关于SQL和ORM的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行了一番讨论,感触还是有一些,于是就有了今天这篇文。声明:本文不会下关于Mybatis和JPA两个持久层框架哪个更好这样的结论。只是摆事实,讲道理,所以,请各位看官勿喷。一、事件起因关于Mybatis和JPA孰优孰劣的问题,争论已经很多年了。一直也没有结论,毕竟每个人的喜好和习惯是大不相同的。我也看…

    2022年10月20日
    0
  • linux命令大全(手册)_Linux高频命令汇总

    linux命令大全(手册)_Linux高频命令汇总史上最全的Linux常用命令汇总(超全面!超详细!)收藏这一篇就够了!

    2022年8月22日
    3

发表回复

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

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