在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ 數(shù)據(jù)分析&挖掘/ Pandas缺失數(shù)據(jù)
Pandas教程
Pandas注意事項(xiàng)&竅門(mén)
Pandas IO工具
Pandas重建索引
Pandas稀疏數(shù)據(jù)
Pandas時(shí)間差(Timedelta)
Pandas聚合
Pandas字符串和文本數(shù)據(jù)
Pandas分類(lèi)數(shù)據(jù)
Pandas索引和選擇數(shù)據(jù)
Pandas基本功能
Pandas系列
Pandas數(shù)據(jù)幀(DataFrame)
Pandas日期功能
Pandas缺失數(shù)據(jù)
Pandas與SQL比較
Pandas迭代
Pandas合并/連接
Pandas選項(xiàng)和自定義
Pandas級(jí)聯(lián)
Pandas可視化
Pandas數(shù)據(jù)結(jié)構(gòu)
Pandas環(huán)境安裝配置
Pandas統(tǒng)計(jì)函數(shù)
Pandas窗口函數(shù)
Pandas面板(Panel)
Pandas排序
Pandas函數(shù)應(yīng)用
Pandas快速入門(mén)
Pandas描述性統(tǒng)計(jì)
Pandas分組(GroupBy)

Pandas缺失數(shù)據(jù)

數(shù)據(jù)丟失(缺失)在現(xiàn)實(shí)生活中總是一個(gè)問(wèn)題。 機(jī)器學(xué)習(xí)和數(shù)據(jù)挖掘等領(lǐng)域由于數(shù)據(jù)缺失導(dǎo)致的數(shù)據(jù)質(zhì)量差,在模型預(yù)測(cè)的準(zhǔn)確性上面臨著嚴(yán)重的問(wèn)題。 在這些領(lǐng)域,缺失值處理是使模型更加準(zhǔn)確和有效的重點(diǎn)。

何時(shí)以及為什么數(shù)據(jù)丟失?

想象一下有一個(gè)產(chǎn)品的在線(xiàn)調(diào)查。很多時(shí)候,人們不會(huì)分享與他們有關(guān)的所有信息。 很少有人分享他們的經(jīng)驗(yàn),但不是他們使用產(chǎn)品多久; 很少有人分享使用產(chǎn)品的時(shí)間,經(jīng)驗(yàn),但不是他們的個(gè)人聯(lián)系信息。 因此,以某種方式或其他方式,總會(huì)有一部分?jǐn)?shù)據(jù)總是會(huì)丟失,這是非常常見(jiàn)的現(xiàn)象。

現(xiàn)在來(lái)看看如何處理使用Pandas的缺失值(如NANaN)。

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)

執(zhí)行上面示例代碼,得到以下結(jié)果 -

        one       two     three
a  0.691764 -0.118095 -0.950871
b       NaN       NaN       NaN
c -0.886898  0.053705 -1.269253
d       NaN       NaN       NaN
e -0.344967 -0.837128  0.730831
f -1.193740  1.767796  0.888104
g       NaN       NaN       NaN
h -0.755934 -1.331638  0.272248

使用重構(gòu)索引(reindexing),創(chuàng)建了一個(gè)缺少值的DataFrame。 在輸出中,NaN表示不是數(shù)字的值。

檢查缺失值

為了更容易地檢測(cè)缺失值(以及跨越不同的數(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['one'].isnull())

執(zhí)行上面示例代碼,得到以下結(jié)果 -

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

執(zhí)行上面示例代碼,得到以下結(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['one'].sum())

執(zhí)行上面示例代碼,得到以下結(jié)果 -

-2.6163354325445014

示例2

import pandas as pd
import numpy as np

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

執(zhí)行上面示例代碼,得到以下結(jié)果 -

nan

清理/填充缺少數(shù)據(jù)

Pandas提供了各種方法來(lái)清除缺失的值。fillna()函數(shù)可以通過(guò)幾種方法用非空數(shù)據(jù)“填充”NA值,在下面的章節(jié)中將學(xué)習(xí)和使用。

用標(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 ("NaN replaced with '0':")
print (df.fillna(0))

執(zhí)行上面示例代碼,得到以下結(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)然,也可以填寫(xiě)任何其他的值。

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

使用重構(gòu)索引章節(jié)討論的填充概念,來(lái)填補(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.fillna(method='pad'))

執(zhí)行上面示例代碼,得到以下結(jié)果 -

        one       two     three
a  0.614938 -0.452498 -2.113057
b  0.614938 -0.452498 -2.113057
c -0.118390  1.333962 -0.037907
d -0.118390  1.333962 -0.037907
e  0.699733  0.502142 -0.243700
f  0.544225 -0.923116 -1.123218
g  0.544225 -0.923116 -1.123218
h -0.669783  1.187865  1.112835

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

執(zhí)行上面示例代碼,得到以下結(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())

執(zhí)行上面示例代碼,得到以下結(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))

執(zhí)行上面示例代碼,得到以下結(jié)果 -

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

替換丟失(或)通用值

很多時(shí)候,必須用一些具體的值取代一個(gè)通用的值。可以通過(guò)應(yīng)用替換方法來(lái)實(shí)現(xiàn)這一點(diǎn)。

用標(biāo)量值替換NAfillna()函數(shù)的等效行為。

示例1

import pandas as pd
import numpy as np
df = pd.DataFrame({'one':[10,20,30,40,50,2000],
'two':[1000,0,30,40,50,60]})
print (df.replace({1000:10,2000:60}))

執(zhí)行上面示例,得到以下結(jié)果 -

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

示例2

import pandas as pd
import numpy as np
df = pd.DataFrame({'one':[10,20,30,40,50,2000],
'two':[1000,0,30,40,50,60]})
print (df.replace({1000:10,2000:60}))

執(zhí)行上面示例代碼,得到以下結(jié)果 -

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