fillna()函数

method:  pad/ffill前一个非缺失值去填充该缺失值,左边的非缺失值去填充该缺失值

         backfill/bfill下一个非缺失值填充该缺失值,右边的非缺失值去填充该缺失值

         None:指定一个值去替换缺失值(默认这种方式)

limit参数:限制填充个数

axis参数:修改填充方向,默认为纵向填充,axis=1的时候,横向填充

import numpy as np
df = pd.DataFrame(  [[np.nan,2,np.nan,np.nan],
                     [3,4,88,np.nan],
                     [np.nan,np.nan,np.nan,'k'],
                     [np.nan,3,np.nan,np.nan]],
                     columns=list('ABCD'))
print(df)
print(df.fillna(100))                    # 用常数填充
print(df.fillna({'A':'这是第一列的填充值'}))# 字典填充
print(df.fillna(method='backfill'))      # 同bfill
print(df.fillna(method='pad'))           # 同ffill
print(df.fillna(method='pad', limit=1))  # limit限制了只填充一个
print(df.fillna(method='pad', axis=1))   # axis为1时,横向填充
#      A    B     C    D
# 0  NaN  2.0   NaN  NaN
# 1  3.0  4.0  88.0  NaN
# 2  NaN  NaN   NaN    k
# 3  NaN  3.0   NaN  NaN
#        A      B      C    D
# 0  100.0    2.0  100.0  100
# 1    3.0    4.0   88.0  100
# 2  100.0  100.0  100.0    k
# 3  100.0    3.0  100.0  100
#            A    B     C    D
# 0  这是第一列的填充值  2.0   NaN  NaN
# 1          3  4.0  88.0  NaN
# 2  这是第一列的填充值  NaN   NaN    k
# 3  这是第一列的填充值  3.0   NaN  NaN
#      A    B     C    D
# 0  3.0  2.0  88.0    k
# 1  3.0  4.0  88.0    k
# 2  NaN  3.0   NaN    k
# 3  NaN  3.0   NaN  NaN
#      A    B     C    D
# 0  NaN  2.0   NaN  NaN
# 1  3.0  4.0  88.0  NaN
# 2  3.0  4.0  88.0    k
# 3  3.0  3.0  88.0    k
#      A    B     C    D
# 0  NaN  2.0   NaN  NaN
# 1  3.0  4.0  88.0  NaN
# 2  3.0  4.0  88.0    k
# 3  NaN  3.0   NaN    k
#      A    B    C   D
# 0  NaN    2    2   2
# 1    3    4   88  88
# 2  NaN  NaN  NaN   k
# 3  NaN    3    3   3
View Code

参考:https://blog.csdn.net/weixin_39549734/article/details/81221276

源码:

1     def fillna(self, value=None, method=None, axis=None, inplace=False,
2                limit=None, downcast=None, **kwargs):
3         return super(DataFrame,
4                      self).fillna(value=value, method=method, axis=axis,
5                                   inplace=inplace, limit=limit,
6                                   downcast=downcast, **kwargs)
7 
8     @Appender(_shared_docs['shift'] % _shared_doc_kwargs)
View Code

 

posted on 2019-05-08 13:11  吃我一枪  阅读(2568)  评论(0编辑  收藏  举报

导航