3-Pandas數據初探索之缺失值處理與丟棄數據(填充fillna()、刪除drop()、drop_duplicates()、dropna())


 一、了解缺失值

  • 通常使用 NA('not available')來代指缺失值
  • Pandas的數據結構中,缺失值使用 NaN('Not a Number')進行標識

除了匯總統計方法,還可以使用isnull()來對數據中缺失的樣本占比、特征大致的缺失情況進行了解。

>>> df =pd.DataFrame({'one':pd.Series([1,2,3],index=['a','b','c']),
...                   'two':pd.Series([1,3,2,7],index=['a','b','c','d']),
...                   'three':pd.Series([3,8,3],index=['d','a','c'])})
>>> df
   one  two  three
a  1.0    1    8.0
b  2.0    3    NaN
c  3.0    2    3.0
d  NaN    7    3.0

#缺失值的數量分析
>>> df.isnull()
     one    two  three
a  False  False  False
b  False  False   True
c  False  False  False
d   True  False  False

>>> df.isnull().sum()
one      1
two      0
three    1
dtype: int64

二、缺失值填充---fillna()

   使用fillna()方法進行缺失值填補

填充方式分為以下幾種:

(1)使用同一個值填補所有的缺失值

>>> df.fillna('用我填充')
    one  two three
a     1    1     8
b     2    3  用我填充
c     3    2     3
d  用我填充    7     3

(2)向前填充、向后填充--->通過設置參數method參數來實現

method參數 說明
ffill或pad 向前填充值
bfill或backfill 向后填充值

 

 

 

 

#向前填充
>>> df.fillna(method='pad')
   one  two  three
a  1.0    1    8.0
b  2.0    3    8.0
c  3.0    2    3.0
d  3.0    7    3.0

#向后填充
>>> df.fillna(method='bfill')
   one  two  three
a  1.0    1    8.0
b  2.0    3    3.0
c  3.0    2    3.0
d  NaN    7    3.0

(3)對不同列的缺失值使用不同的值進行填補

  可以使用字典的方式,如下:

>>> df.fillna({'one':1,'three':3})
   one  two  three
a  1.0    1    8.0
b  2.0    3    3.0
c  3.0    2    3.0
d  1.0    7    3

(4)使用一個Pandas的自動對齊功能進行填補

   這也是最常使用的一種方式

>>> df.fillna(df.mean())
   one  two     three
a  1.0    1  8.000000
b  2.0    3  4.666667
c  3.0    2  3.000000
d  2.0    7  3.000000

三、丟棄數據

  在一些實際的應用場景中,需要根據某些過濾條件丟棄部分無用數據,在多數情況下數據過濾可以同時達到獲取和丟棄數據的目的

  除了使用布爾表達式之外,Pandas對象還有以下三種方法來丟棄無用數據:

  1. drop():根據標簽丟棄數據
  2. drop_duplicates():丟棄重復數據
  3. dropna():丟失缺失數據

(1)根據標簽丟棄數據----drop()

  drop()可以根據標簽丟棄多行或多了數據,基本參數如下:

    labels:單個或者多個標簽,傳入類列表值(列表、array等)

    axis:丟棄行(0,默認)或者列(1)

    inplace:是否用結果替換原pandas對象(默認False)

#仍然以上述的df為例
>>> df
   one  two  three
a  1.0    1    8.0
b  2.0    3    NaN
c  3.0    2    3.0
d  NaN    7    3.0

>>> df.drop(['d','c'])
   one  two  three
a  1.0    1    8.0
b  2.0    3    NaN

>>> df.drop(labels=['d','c'])
   one  two  three
a  1.0    1    8.0
b  2.0    3    NaN

>>> df.drop(labels=['three'],axis=1)
   one  two
a  1.0    1
b  2.0    3
c  3.0    2
d  NaN    7

 (2)丟棄重復數據----drop_duplicates()

  1)由於不同的原因,數據中可能會包含重復出現的行(記錄),重復的記錄會造成信息的冗余,但是在實際中丟棄重復數據需要謹慎,盲目去重可以會造成數據集丟失部分數據。

  duplicated()方法可以返回一個布爾型的Series,表示各行是否重復,僅僅將重復的最后一行標記為True;

  注:duplicated()方法有參數keep,keep的值有一下三種情況:

    keep = 'first'---->返回結果中的第一個重復的數據

    keep = 'last'----->返回結果中的最后一個重復的數據

    keep = False------>所有的重復數據為True

#先創建一個new_df,與df中有重復的記錄
>>> new =pd.DataFrame({'one':pd.Series([1,3],index=['e','f']),'two':pd.Series([1,2],index=['e','f']),'three':pd.Series([8,3],index=['e','f'])})
>>> new
   one  two  three
e    1    1      8
f    3    2      3
>>> new_df = pd.concat([df,new])
>>> new_df
   one  two  three
a  1.0    1    8.0
b  2.0    3    NaN
c  3.0    2    3.0
d  NaN    7    3.0
e  1.0    1    8.0
f  3.0    2    3.0

#查看各行是否重復
>>> new_df.duplicated()
a    False
b    False
c    False
d    False
e     True
f     True
dtype: bool

#通過布爾過濾可以丟棄重復數據
>>> new_df[new_df.duplicated()==False]
   one  two  three
a  1.0    1    8.0
b  2.0    3    NaN
c  3.0    2    3.0
d  NaN    7    3.0

>>> new_df.duplicated(keep='first')
a    False
b    False
c    False
d    False
e     True
f     True
dtype: bool
>>> new_df.duplicated(keep=False)
a     True
b    False
c     True
d    False
e     True
f     True
dtype: bool

  2)Pandas提供的drop_duplicates()可以更加簡便的完成去重操作,默認情況下會判斷全部列,若只希望根據某一列判斷重復項,則在括號后加入列即可。

>>> new_df.drop_duplicates()
   one  two  three
a  1.0    1    8.0
b  2.0    3    NaN
c  3.0    2    3.0
d  NaN    7    3.0

>>> new_df.drop_duplicates('three')
   one  two  three
a  1.0    1    8.0
b  2.0    3    NaN
c  3.0    2    3.0
>>> new_df.drop_duplicates(['one','three'])
   one  two  three
a  1.0    1    8.0
b  2.0    3    NaN
c  3.0    2    3.0
d  NaN    7    3.0

  (3)丟棄缺失值數據----dropna()

  在含有缺失值樣本比例較小的情況下,可以考慮丟棄缺失值所在的行,或者說一個特征大部分都是缺失值是,也可以考慮丟棄該特征。

  dropna()方法用於丟棄缺失值相關數據,其常用的參數如下:

    axis:丟棄缺失值所在的行(0,默認)或所在列(1);

    how:'any'表示只要存在缺失值就丟棄(默認),'all'表示所有的值均為缺失值才丟棄;

    subset:考慮部分行和列

    inplace:是否替換原來的Pandas對象

#仍以df為例,此時若要刪除存在缺失值所在的行,則
>>> df.dropna(how='any')
   one  two  three
a  1.0    1    8.0
c  3.0    2    3.0

#若刪除three列,則
>>> df.drop(columns='three',inplace=True)
>>> df
   one  two
a  1.0    1
b  2.0    3
c  3.0    2
d  NaN    7

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM