成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

怎么用pandas處理缺失值

這篇文章主要介紹怎么用pandas處理缺失值,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站制作、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的武鳴網(wǎng)站設(shè)計(jì)、移動媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

pandas對象的所有描述性統(tǒng)計(jì)信息默認(rèn)情況下是排除缺失值的。

pandas對象中表現(xiàn)缺失值的方式并不完美,但是它對大部分用戶來說是有用的。對于數(shù)值型數(shù)據(jù),pandas使用浮點(diǎn)值NaN(Not a Number來表示缺失值)。我們稱NaN為容易檢測到的標(biāo)識值:

In : 

string_data = pd.Series(['aardvark', 'artichoke', np.nan, 'avocado'])
string_data

Out:

0      aardvark  1     artichoke  2            NaN  3       avocado  dtype: object

In:

string_data.isnull()

Out:

0     False  1     False  2      True  3     False  dtype: bool

在pandas中,我們采用了R語言中的編程慣例,將缺失值成為NA,意思是not available(不可用)。在統(tǒng)計(jì)學(xué)應(yīng)用中,NA數(shù)據(jù)可以是不存在的數(shù)據(jù)或者是存在但不可觀察的數(shù)據(jù)(例如在數(shù)據(jù)收集過程中出現(xiàn)了問題)。當(dāng)清洗數(shù)據(jù)用于分析時(shí),對缺失數(shù)據(jù)本身進(jìn)行分析以確定數(shù)據(jù)收集問題或數(shù)據(jù)丟失導(dǎo)致的數(shù)據(jù)偏差通常很重要。

Python內(nèi)建的None值在對象數(shù)組中也被當(dāng)作NA處理:

In:

string_data[0] = None
string_data.isnull()

Out:

0      True  1     False  2      True  3     False  dtype: bool

pandas項(xiàng)目持續(xù)改善處理缺失值的內(nèi)部細(xì)節(jié),但是用戶API函數(shù),比如pandas. isnull,抽象掉了很多令人厭煩的細(xì)節(jié)。處理缺失值的相關(guān)函數(shù)列表如下:

  •  dropna:根據(jù)每個(gè)標(biāo)簽的值是否是缺失數(shù)據(jù)來篩選軸標(biāo)簽,并根據(jù)允許丟失的數(shù)據(jù)量來確定閾值

  •  fillna:用某些值填充缺失的數(shù)據(jù)或使用插值方法(如“ffill”或“bfill”)。

  •  isnull:返回表明哪些值是缺失值的布爾值

  •  notnull:isnull的反作用函數(shù)

01 過濾缺失值

有多種過濾缺失值的方法。雖然你可以使用pandas.isnull和布爾值索引手動地過濾缺失值,但dropna在過濾缺失值時(shí)是非常有用的。在Series上使用dropna,它會返回Series中所有的非空數(shù)據(jù)及其索引值:

In:

from numpy import nan as NA
data = pd.Series([1, NA, 3.5, NA, 7])
data.dropna()

Out:

0     1.0  2     3.5  4     7.0  dtype: float64

上面的例子與下面的代碼是等價(jià)的:

In:

data[data.notnull()]

Out:

0     1.0  2     3.5  4     7.0  dtype: float64

當(dāng)處理DataFrame對象時(shí),事情會稍微更復(fù)雜一點(diǎn)。你可能想要刪除全部為NA或包含有NA的行或列。dropna默認(rèn)情況下會刪除包含缺失值的行:

In:

data = pd.DataFrame([[1., 6.5, 3.], [1., NA, NA]                       [NA, NA, NA], [NA, 6.5, 3.]])
cleaned = data.dropna()
data

Out:

   0     1     2  0  1.0  6.5  3.0  1  1.0  NaN  NaN  2  NaN  NaN  NaN  3  NaN  6.5  3.0

In:

cleaned

Out:

0     1     2   1.0  6.5  3.0

傳入how='all’時(shí),將刪除所有值均為NA的行:

In:

data.dropna(how='all')

Out:

     0    1    2  0  1.0  6.5  3.0  1  1.0  NaN  NaN  3  NaN  6.5  3.0

如果要用同樣的方式去刪除列,傳入?yún)?shù)axis=1:

In:

data[4] = NA
data

Out:

     0    1    2   4  0  1.0  6.5  3.0 NaN  1  1.0  NaN  NaN NaN  2  NaN  NaN  NaN NaN  3  NaN  6.5  3.0 NaN

In:

data.dropna(axis=1, how='all')

Out:

     0    1    2  0  1.0  6.5  3.0  1  1.0  NaN  NaN  2  NaN  NaN  NaN  3  NaN  6.5  3.0

過濾DataFrame的行的相關(guān)方法往往涉及時(shí)間序列數(shù)據(jù)。假設(shè)你只想保留包含一定數(shù)量的觀察值的行。你可以用thresh參數(shù)來表示:

In:

df = pd.DataFrame(np.random.randn(7, 3))
df.iloc[:4, 1] = NA
df.iloc[:2, 2] = NA
df

Out:

          0         1         2  0 -0.204708       NaN       NaN  1 -0.555730       NaN       NaN  2  0.092908       NaN  0.769023  3  1.246435       NaN -1.296221  4  0.274992  0.228913  1.352917  5  0.886429 -2.001637 -0.371843  6  1.669025 -0.438570 -0.539741

In:

df.dropna()

Out:

         0         1         2  4 0.274992  0.228913  1.352917  5 0.886429 -2.001637 -0.371843  6 1.669025 -0.438570 -0.539741

In:

df.dropna(thresh=2)

Out:

         0         1         2  2 0.092908       NaN  0.769023  3 1.246435       NaN -1.296221  4 0.274992  0.228913  1.352917  5 0.886429 -2.001637 -0.371843  6 1.669025 -0.438570 -0.539741

02 補(bǔ)全缺失值

你有時(shí)可能需要以多種方式補(bǔ)全“漏洞”,而不是過濾缺失值(也可能丟棄其他數(shù)據(jù))。

大多數(shù)情況下,主要使用fillna方法來補(bǔ)全缺失值。調(diào)用fillna時(shí),可以使用一個(gè)常數(shù)來替代缺失值:

In:

df.fillna(0)

Out:

          0         1         2  0 -0.204708  0.000000  0.000000  1 -0.555730  0.000000  0.000000  2  0.092908  0.000000  0.769023  3  1.246435  0.000000 -1.296221  4  0.274992  0.228913  1.352917  5  0.886429 -2.001637 -0.371843  6  1.669025 -0.438570 -0.539741

在調(diào)用fillna時(shí)使用字典,你可以為不同列設(shè)定不同的填充值:

In:

df.fillna({1: 0.5, 2: 0})

Out:

         0         1         2  0 -0.204708  0.500000  0.000000  1 -0.555730  0.500000  0.000000  2  0.092908  0.500000  0.769023  3  1.246435  0.500000 -1.296221  4  0.274992  0.228913  1.352917  5  0.886429 -2.001637 -0.371843  6  1.669025 -0.438570 -0.539741

fillna返回的是一個(gè)新的對象,但你也可以修改已經(jīng)存在的對象:

In:

_ = df.fillna(0, inplace=True)
df

Out:

        0         1         2  0 -0.204708  0.000000  0.000000  1 -0.555730  0.000000  0.000000  2  0.092908  0.000000  0.769023  3  1.246435  0.000000 -1.296221  4  0.274992  0.228913  1.352917  5  0.886429 -2.001637 -0.371843  6  1.669025 -0.438570 -0.539741

用于重建索引的相同的插值方法也可以用于fillna:

In:

df = pd.DataFrame(np.random.randn(6, 3))
df.iloc[2:, 1] = NA
df.iloc[4:, 2] = NA
df

Out:

         0         1         2  0  0.476985  3.248944 -1.021228  1 -0.577087  0.124121  0.302614  2  0.523772       NaN  1.343810  3 -0.713544       NaN -2.370232  4 -1.860761       NaN       NaN  5 -1.265934       NaN       NaN

In:

df.fillna(method='ffill')

Out:

          0         1         2  0  0.476985  3.248944 -1.021228  1 -0.577087  0.124121  0.302614  2  0.523772  0.124121  1.343810  3 -0.713544  0.124121 -2.370232  4 -1.860761  0.124121 -2.370232  5 -1.265934  0.124121 -2.370232

In:

df.fillna(method='ffill', limit=2)

Out:

        0         1         2  0  0.476985  3.248944 -1.021228  1 -0.577087  0.124121  0.302614  2  0.523772  0.124121  1.343810  3 -0.713544  0.124121 -2.370232  4 -1.860761       NaN -2.370232  5 -1.265934       NaN -2.370232

使用fillna你可以完成很多帶有一點(diǎn)創(chuàng)造性的工作。例如,你可以將Series的平均值或中位數(shù)用于填充缺失值:

In:

data = pd.Series([1., NA, 3.5, NA, 7])
data.fillna(data.mean())

Out:

0     1.000000  1     3.833333  2     3.500000  3     3.833333  4     7.000000  dtype: float64

以下是fillna的函數(shù)參數(shù)。

  •  value:標(biāo)量值或字典型對象用于填充缺失值

  •  method:插值方法,如果沒有其他參數(shù),默認(rèn)是'ffill'

  •  axis:需要填充的軸,默認(rèn)axis=0

  •  inplace:修改被調(diào)用的對象,而不是生成一個(gè)備份

  •  limit:用于前向或后向填充時(shí)最大的填充范圍

以上是“怎么用pandas處理缺失值”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

新聞名稱:怎么用pandas處理缺失值
網(wǎng)站URL:http://jinyejixie.com/article16/ggiodg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、網(wǎng)頁設(shè)計(jì)公司、建站公司、App設(shè)計(jì)、虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)