日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

pandas中fillna()函數(shù)填充NaN和None的實(shí)現(xiàn)_python

作者:一杯冰糖 ? 更新時(shí)間: 2023-03-15 編程語言

填充缺失值和空值的方式有很多種,比如人工填寫、熱卡填充等,Pandas中的fillna()方法可以實(shí)現(xiàn)填充空值或缺失值。

fillna(value=None, method=None, axis=None, inplace=False,
limit=None, downcast=None, **kwargs)
  • value:用于填充的數(shù)值。
  • method:表示填充方式,默認(rèn)值為None。
  • limit: 可以連續(xù)填充的最大數(shù)量,默認(rèn)None。

method參數(shù)不能與value參數(shù)同時(shí)使用。

name score
agou NaN
None 78.0
ahua 89.0

有一張表格里存在缺失值,如果使用常量99.0來替換缺失值,那么填充前后的效果如下圖所示。

name score
agou 99.0
99.0 78.0
ahua 89.0

通過fillna()方法填充常量的示例如下:

# 使用99.0替換缺失值
df_obj.fillna('99.0')

顯然name列不適合用99.0來填充,我們可以指定某列的填充值

# 指定列填充數(shù)據(jù)
df_obj.fillna({'name': 'someone', 'score': 99.0})

結(jié)果:

name score
agou 99.0
someone 78.0
ahua 89.0

通過fillna()方法采用前向填充的方式替換空值或缺失值,示例如下:

# 使用前向填充的方式替換空值或缺失值
df.fillna(method='ffill')

原文鏈接:https://blog.csdn.net/qq_45186086/article/details/125218954

欄目分類
最近更新