亚洲区国产区激情区无码区,国产成人mv视频在线观看,国产A毛片AAAAAA,亚洲精品国产首次亮相在线

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

Pandas 數(shù)據(jù)丟失的操作實(shí)例

在現(xiàn)實(shí)生活中,數(shù)據(jù)丟失始終是一個(gè)問題。機(jī)器學(xué)習(xí)和數(shù)據(jù)挖掘等領(lǐng)域在模型預(yù)測的準(zhǔn)確性方面面臨嚴(yán)重問題,因?yàn)槿鄙僦禃?dǎo)致數(shù)據(jù)質(zhì)量較差。在這些領(lǐng)域中,缺失值處理是使模型更準(zhǔn)確和有效的主要重點(diǎn)。

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

讓我們考慮一項(xiàng)產(chǎn)品的在線調(diào)查。很多時(shí)候,人們不會共享與他們有關(guān)的所有信息。很少有人會分享他們的經(jīng)驗(yàn),但是不會分享他們使用該產(chǎn)品有多長時(shí)間;很少有人分享他們使用該產(chǎn)品的時(shí)間,他們的經(jīng)歷而不是他們的聯(lián)系信息。因此,以某種方式或其他方式總是會丟失一部分?jǐn)?shù)據(jù),這在實(shí)時(shí)情況下非常普遍。
現(xiàn)在讓我們看看如何使用熊貓?zhí)幚砣笔е担ɡ鏝A或NaN)。

# import the pandas library
 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)

運(yùn)行結(jié)果如下:

       one        two     three
a  -0.576991  -0.741695  0.553172
b        NaN        NaN       NaN
c   0.744328  -1.735166  1.749580

NaN replaced with '0':
         one        two     three
a  -0.576991  -0.741695  0.553172
b   0.000000   0.000000  0.000000
c   0.744328  -1.735166  1.749580

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

檢查缺失值

為了使檢測的缺失值更容易(和不同陣列dtypes),熊貓?zhí)峁㊣SNULL()和NOTNULL()功能,這也是對系列和數(shù)據(jù)幀的對象的方法-

實(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'].isnull())

運(yùn)行結(jié)果如下:

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

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

運(yùn)行結(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將被視為零 如果數(shù)據(jù)均為不適用,則結(jié)果為不適用

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

運(yùn)行結(jié)果如下:

   2.02357685917

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

運(yùn)行結(jié)果如下:

   nan

清理/填充丟失的數(shù)據(jù)

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

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

以下程序顯示了如何將“ NaN”替換為“ 0”。

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

運(yùn)行結(jié)果如下:

       one        two     three
a  -0.576991  -0.741695  0.553172
b        NaN        NaN       NaN
c   0.744328  -1.735166  1.749580

NaN replaced with '0':
         one        two     three
a  -0.576991  -0.741695  0.553172
b   0.000000   0.000000  0.000000
c   0.744328  -1.735166  1.749580

在這里,我們填充零值;相反,我們還可以填充其他任何值。

向前和向后填充NA

使用“重新索引”一章中討論的填充概念,我們將填充缺少的值。

方法操作
pad/fill向前填充<
bfill/backfill向后填充

實(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.fillna(method='pad'))

運(yùn)行結(jié)果如下:

       one        two      three
a   0.077988   0.476149   0.965836
b   0.077988   0.476149   0.965836
c  -0.390208  -0.551605  -2.301950
d  -0.390208  -0.551605  -2.301950
e  -2.000303  -0.788201   1.510072
f  -0.930230  -0.670473   1.146615
g  -0.930230  -0.670473   1.146615
h   0.085100   0.532791   0.887415

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

運(yùn)行結(jié)果如下:

       one        two      three
a   0.077988   0.476149   0.965836
b  -0.390208  -0.551605  -2.301950
c  -0.390208  -0.551605  -2.301950
d  -2.000303  -0.788201   1.510072
e  -2.000303  -0.788201   1.510072
f  -0.930230  -0.670473   1.146615
g   0.085100   0.532791   0.887415
h   0.085100   0.532791   0.887415

刪除缺失值

如果只想排除丟失的值,則將dropna函數(shù)與axis參數(shù)一起使用。默認(rèn)情況下,axis = 0,即沿著行,這意味著如果一行中的任何值為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.dropna())

運(yùn)行結(jié)果如下:

  
   one two three a 0.077988 0.476149 0.965836 c -0.390208 -0.551605 -2.301950 e -2.000303 -0.788201 1.510072 f -0.930230 -0.670473 1.146615 h 0.085100 0.532791 0.887415

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

運(yùn)行結(jié)果如下:

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

替換缺失的(或)通用值

很多時(shí)候,我們必須用某個(gè)特定值替換一個(gè)通用值。我們可以通過應(yīng)用replace方法來實(shí)現(xiàn)。
用標(biāo)量值替換NA是fillna()函數(shù)的等效行為。

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

運(yùn)行結(jié)果如下:

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

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

運(yùn)行結(jié)果如下:

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