欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pandas 缺失數(shù)據(jù)處理的實現(xiàn)

 更新時間:2019年11月04日 09:28:54   作者:PythonGirl  
這篇文章主要介紹了Pandas 缺失數(shù)據(jù)處理的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

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

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

一、檢查缺失值

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

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

輸出結(jié)果:

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

輸出結(jié)果:
a     True
b    False
c     True
d    False
e     True
f     True
g    False
h     True
Name: one, dtype: bool

二、缺少數(shù)據(jù)的計算

  • 在求和數(shù)據(jù)時,NA將被視為0
  • 如果數(shù)據(jù)全部是NA,那么結(jié)果將是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())

輸出結(jié)果:

        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 

示例2

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

輸出結(jié)果:

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

0

三、填充缺少數(shù)據(jù)

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

用標(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('\n')

print ("NaN replaced with '0':")
print (df.fillna(0))

輸出結(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)然,也可以填寫任何其他的值。

替換丟失(或)通用值

很多時候,必須用一些具體的值取代一個通用的值??梢酝ㄟ^應(yīng)用替換方法來實現(xiàn)這一點。用標(biāo)量值替換NA是fillna()函數(shù)的等效行為。

示例

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

輸出結(jié)果:

    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前進(jìn)和后退

使用重構(gòu)索引章節(jié)討論的填充概念,來填補(bǔ)缺失的值。

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

輸出結(jié)果:

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

輸出結(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,那么整個行被排除。

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

輸出結(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))

輸出結(jié)果:

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Pycharm報錯:'NoneType'?object?has?no?attribute?'bytes'的解決方法

    Pycharm報錯:'NoneType'?object?has?no?attribute?

    這篇文章主要給大家介紹了關(guān)于Pycharm報錯:'NoneType'?object?has?no?attribute?'bytes'的解決方法,文中通過圖文將解決的方法介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02
  • Python3.x檢查內(nèi)存可用大小的兩種實現(xiàn)

    Python3.x檢查內(nèi)存可用大小的兩種實現(xiàn)

    本文將介紹如何使用Python 3實現(xiàn)檢查Linux服務(wù)器內(nèi)存可用大小的方法,包括使用Python標(biāo)準(zhǔn)庫實現(xiàn)和使用Linux命令實現(xiàn)兩種方式,感興趣可以了解一下
    2023-05-05
  • Python實現(xiàn)12種降維算法的示例代碼

    Python實現(xiàn)12種降維算法的示例代碼

    數(shù)據(jù)降維算法是機(jī)器學(xué)習(xí)算法中的大家族,與分類、回歸、聚類等算法不同,它的目標(biāo)是將向量投影到低維空間,以達(dá)到某種目的如可視化,或是做分類。本文將利用Python實現(xiàn)12種降維算法,需要的可以參考一下
    2022-04-04
  • PyCharm取消波浪線、下劃線和中劃線的實現(xiàn)

    PyCharm取消波浪線、下劃線和中劃線的實現(xiàn)

    這篇文章主要介紹了PyCharm取消波浪線、下劃線和中劃線的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • python逐行讀寫txt文件的實例講解

    python逐行讀寫txt文件的實例講解

    下面小編就為大家分享一篇python逐行讀寫txt文件的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • 詳解Python在七牛云平臺的應(yīng)用(一)

    詳解Python在七牛云平臺的應(yīng)用(一)

    這篇文章主要介紹了詳解Python在七牛云平臺的應(yīng)用(一),涉及Python通過官方庫對空間的操作,上傳的步驟,操作方法等相關(guān)內(nèi)容,以及完整的操作代碼,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • python遞歸刪除指定目錄及其所有內(nèi)容的方法

    python遞歸刪除指定目錄及其所有內(nèi)容的方法

    下面小編就為大家?guī)硪黄猵ython遞歸刪除指定目錄及其所有內(nèi)容的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • python簡單實現(xiàn)9宮格圖片實例

    python簡單實現(xiàn)9宮格圖片實例

    在本篇內(nèi)容里小編給各位分享的是一篇關(guān)于python實現(xiàn)朋友圈中的九宮格圖片的實例講解,有需要的朋友們可以參考下。
    2020-09-09
  • Python實現(xiàn)圖像增強(qiáng)

    Python實現(xiàn)圖像增強(qiáng)

    這篇文章主要為大家詳細(xì)介紹了Python實現(xiàn)圖像增強(qiáng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • 利用Python實現(xiàn)一個簡單的Web匯率計算器

    利用Python實現(xiàn)一個簡單的Web匯率計算器

    Dash?是一個用于構(gòu)建基于?Web?的應(yīng)用程序的?Python?庫,無需?JavaScript?。本文將利用Dash編寫一個簡單的Web匯率計算器,感興趣的可以了解一下
    2022-08-08

最新評論