Pandas | 17 缺失數據處理

數據丟失(缺失)在現實生活中老是一個問題。 機器學習和數據挖掘等領域因爲數據缺失致使的數據質量差,在模型預測的準確性上面臨着嚴重的問題。 在這些領域,缺失值處理是使模型更加準確和有效的重點。python

 

使用重構索引(reindexing),建立了一個缺乏值的DataFrame。 在輸出中,NaN表示不是數字的值shell

1、檢查缺失值

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

示例1dom

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())

輸出結果:機器學習

        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())
輸出結果:
a     True
b False
c True
d False
e True
f True
g False
h True
Name: one, dtype: bool
 

2、缺乏數據的計算

  • 在求和數據時,NA將被視爲0
  • 若是數據所有是NA,那麼結果將是NA

實例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())

輸出結果:spa

        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

 

示例2code

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())

輸出結果:對象

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

0
 

3、填充缺乏數據

Pandas提供了各類方法來清除缺失的值。fillna()函數能夠經過幾種方法用非空數據「填充」NA值。

 

用標量值替換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))

輸出結果:

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
 

在這裏填充零值; 固然,也能夠填寫任何其餘的值。

 

替換丟失(或)通用值

不少時候,必須用一些具體的值取代一個通用的值。能夠經過應用替換方法來實現這一點。用標量值替換NAfillna()函數的等效行爲。

示例

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}))

輸出結果:

    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前進和後退

使用重構索引章節討論的填充概念,來填補缺失的值。

方法 動做
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'))

輸出結果:

        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'))

輸出結果:

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
 

4、丟失缺乏的值

使用dropna函數和axis參數。 默認狀況下,axis = 0,即在行上應用,這意味着若是行內的任何值是NA,那麼整個行被排除。

實例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())
輸出結果 :
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))
輸出結果:
Empty DataFrame
Columns: []
Index: [a, b, c, d, e, f, g, h]
相關文章
相關標籤/搜索