使用Pandas怎么處理缺失的數(shù)據(jù)-創(chuàng)新互聯(lián)

這篇文章給大家介紹使用Pandas怎么處理缺失的數(shù)據(jù),內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

10年積累的成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先制作網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有正安免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

一、檢查缺失值

為了更容易地檢測缺失值(以及跨越不同的數(shù)組dtype),Pandas提供了isnull()和notnull()函數(shù),它們也是Series和DataFrame對(duì)象的方法

示例1

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 3),
         index=['a', 'c', 'e', 'f','h'],
         columns=['one', 'two', 'three'])

df = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])

print(df)
print('\n')

print (df['one'].isnull())

輸出結(jié)果:

        one       two     three
a  0.036297 -0.615260 -1.341327
b       NaN       NaN       NaN
c -1.908168 -0.779304  0.212467
d       NaN       NaN       NaN
e  0.527409 -2.432343  0.190436
f  1.428975 -0.364970  1.084148
g       NaN       NaN       NaN
h  0.763328 -0.818729  0.240498


a    False
b     True
c    False
d     True
e    False
f    False
g     True
h    False
Name: one, dtype: bool

示例2

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f',
'h'],columns=['one', 'two', 'three'])

df = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])

print (df['one'].notnull())

輸出結(jié)果:
a     True
b    False
c     True
d    False
e     True
f     True
g    False
h     True
Name: one, dtype: bool

二、缺少數(shù)據(jù)的計(jì)算

  • 在求和數(shù)據(jù)時(shí),NA將被視為0

  • 如果數(shù)據(jù)全部是NA,那么結(jié)果將是NA

實(shí)例1

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f',
'h'],columns=['one', 'two', 'three'])

df = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])

print(df)
print('\n')

print (df['one'].sum())

輸出結(jié)果:

        one       two     three
a -1.191036  0.945107 -0.806292
b       NaN       NaN       NaN
c  0.127794 -1.812588 -0.466076
d       NaN       NaN       NaN
e  2.358568  0.559081  1.486490
f -0.242589  0.574916 -0.831853
g       NaN       NaN       NaN
h -0.328030  1.815404 -1.706736


0.7247067964060545 

示例2

import pandas as pd

df = pd.DataFrame(index=[0,1,2,3,4,5],columns=['one','two'])

print(df)
print('\n')

print (df['one'].sum())

輸出結(jié)果:

   one  two
0  NaN  NaN
1  NaN  NaN
2  NaN  NaN
3  NaN  NaN
4  NaN  NaN
5  NaN  NaN

0

三、填充缺少數(shù)據(jù)

Pandas提供了各種方法來清除缺失的值。fillna()函數(shù)可以通過幾種方法用非空數(shù)據(jù)“填充”NA值。

用標(biāo)量值替換NaN

以下程序顯示如何用0替換NaN。

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(3, 3), index=['a', 'c', 'e'],columns=['one','two', 'three'])

df = df.reindex(['a', 'b', 'c'])

print (df)
print('\n')

print ("NaN replaced with '0':")
print (df.fillna(0))

輸出結(jié)果:

        one       two     three
a -0.479425 -1.711840 -1.453384
b       NaN       NaN       NaN
c -0.733606 -0.813315  0.476788

NaN replaced with '0':
        one       two     three
a -0.479425 -1.711840 -1.453384
b  0.000000  0.000000  0.000000
c -0.733606 -0.813315  0.476788

在這里填充零值; 當(dāng)然,也可以填寫任何其他的值。

替換丟失(或)通用值

很多時(shí)候,必須用一些具體的值取代一個(gè)通用的值??梢酝ㄟ^應(yīng)用替換方法來實(shí)現(xiàn)這一點(diǎn)。用標(biāo)量值替換NA是fillna()函數(shù)的等效行為。

示例

import pandas as pd

df = pd.DataFrame({'one':[10,20,30,40,50,2000],'two':[1000,0,30,40,50,60]})

print(df)
print('\n')

print (df.replace({1000:10,2000:60}))

輸出結(jié)果:

    one   two
0    10  1000
1    20     0
2    30    30
3    40    40
4    50    50
5  2000    60

   one  two
0   10   10
1   20    0
2   30   30
3   40   40
4   50   50
5   60   60

填寫NA前進(jìn)和后退

使用重構(gòu)索引章節(jié)討論的填充概念,來填補(bǔ)缺失的值。

方法動(dòng)作
pad/fill填充方法向前
bfill/backfill填充方法向后

示例1

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f',
'h'],columns=['one', 'two', 'three'])

df = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])

print(df)
print('\n')

print (df.fillna(method='pad'))

輸出結(jié)果:

        one       two     three
a -0.023243  1.671621 -1.687063
b       NaN       NaN       NaN
c -0.933355  0.609602 -0.620189
d       NaN       NaN       NaN
e  0.151455 -1.324563 -0.598897
f  0.605670 -0.924828 -1.050643
g       NaN       NaN       NaN
h  0.892414 -0.137194 -1.101791


        one       two     three
a -0.023243  1.671621 -1.687063
b -0.023243  1.671621 -1.687063
c -0.933355  0.609602 -0.620189
d -0.933355  0.609602 -0.620189
e  0.151455 -1.324563 -0.598897
f  0.605670 -0.924828 -1.050643
g  0.605670 -0.924828 -1.050643
h  0.892414 -0.137194 -1.101791

示例2

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f',
'h'],columns=['one', 'two', 'three'])

df = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])

print (df.fillna(method='backfill'))

輸出結(jié)果:

        one       two     three
a  2.278454  1.550483 -2.103731
b -0.779530  0.408493  1.247796
c -0.779530  0.408493  1.247796
d  0.262713 -1.073215  0.129808
e  0.262713 -1.073215  0.129808
f -0.600729  1.310515 -0.877586
g  0.395212  0.219146 -0.175024
h  0.395212  0.219146 -0.175024

四、丟失缺少的值

使用dropna函數(shù)和axis參數(shù)。 默認(rèn)情況下,axis = 0,即在行上應(yīng)用,這意味著如果行內(nèi)的任何值是NA,那么整個(gè)行被排除。

實(shí)例1

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f','h'],columns=['one', 'two', 'three'])

df = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])

print (df.dropna())

輸出結(jié)果 :

        one       two     three
a -0.719623  0.028103 -1.093178
c  0.040312  1.729596  0.451805
e -1.029418  1.920933  1.289485
f  1.217967  1.368064  0.527406
h  0.667855  0.147989 -1.035978

示例2

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f',
'h'],columns=['one', 'two', 'three'])

df = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])

print (df.dropna(axis=1))

輸出結(jié)果:

Empty DataFrame
Columns: []
Index: [a, b, c, d, e, f, g, h]

關(guān)于使用Pandas怎么處理缺失的數(shù)據(jù)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

當(dāng)前標(biāo)題:使用Pandas怎么處理缺失的數(shù)據(jù)-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://bm7419.com/article42/dgddec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、網(wǎng)站制作、全網(wǎng)營銷推廣、微信小程序、網(wǎng)站導(dǎo)航品牌網(wǎng)站設(shè)計(jì)

廣告

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

綿陽服務(wù)器托管