Pandas 數(shù)據(jù)丟失的操作實例
在現(xiàn)實生活中,數(shù)據(jù)丟失始終是一個問題。機器學習和數(shù)據(jù)挖掘等領域在模型預測的準確性方面面臨嚴重問題,因為缺少值會導致數(shù)據(jù)質(zhì)量較差。在這些領域中,缺失值處理是使模型更準確和有效的主要重點。
讓我們考慮一項產(chǎn)品的在線調(diào)查。很多時候,人們不會共享與他們有關的所有信息。很少有人會分享他們的經(jīng)驗,但是不會分享他們使用該產(chǎn)品有多長時間;很少有人分享他們使用該產(chǎn)品的時間,他們的經(jīng)歷而不是他們的聯(lián)系信息。因此,以某種方式或其他方式總是會丟失一部分數(shù)據(jù),這在實時情況下非常普遍。
現(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)
運行結(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)建了一個缺少值的DataFrame。在輸出中,NaN表示不是數(shù)字。
為了使檢測的缺失值更容易(和不同陣列dtypes),熊貓?zhí)峁㊣SNULL()和NOTNULL()功能,這也是對系列和數(shù)據(jù)幀的對象的方法-
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())
運行結(jié)果如下:
a False b True c False d True e False f False g True h False Name: one, dtype: bool
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ù)時,NA將被視為零 如果數(shù)據(jù)均為不適用,則結(jié)果為不適用
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())
運行結(jié)果如下:
2.02357685917
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()
運行結(jié)果如下:
nan
Pandas 提供了多種清除缺失值的方法。fillna函數(shù)可以通過以下幾種方法用非空數(shù)據(jù)“填充” NA值。
以下程序顯示了如何將“ 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))
運行結(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
在這里,我們填充零值;相反,我們還可以填充其他任何值。
使用“重新索引”一章中討論的填充概念,我們將填充缺少的值。
方法 | 操作 |
pad/fill | 向前填充< |
bfill/backfill | 向后填充 |
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'))
運行結(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
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 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ù)一起使用。默認情況下,axis = 0,即沿著行,這意味著如果一行中的任何值為NA,那么將排除整行。
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.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
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]
很多時候,我們必須用某個特定值替換一個通用值。我們可以通過應用replace方法來實現(xiàn)。
用標量值替換NA是fillna()函數(shù)的等效行為。
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}))
運行結(jié)果如下:
one two 0 10 10 1 20 0 2 30 30 3 40 40 4 50 50 5 60 60
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})
運行結(jié)果如下:
one two 0 10 10 1 20 0 2 30 30 3 40 40 4 50 50 5 60 60