pandas中關(guān)于nan的處理方式
pandas關(guān)于nan的處理
在pandas中有個(gè)另類的存在就是nan
解釋是
not a number,不是一個(gè)數(shù)字,但是它的類型確是一個(gè)float類型。
numpy中也存在關(guān)于nan的方法
如:np.nan
對(duì)于pandas中nan的處理,簡(jiǎn)單的說有以下幾個(gè)方法。
- 查看是否是nan, s1.isnull() 和 s1.notnull()
- 丟棄有nan的索引項(xiàng),s1.dropna()
- 將nan填充為其他值,df2.fillna()
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
n = np.nan
print(type(n)) # <class 'float'>
m = 1
print(n+m) # nan 任何數(shù)字和nan進(jìn)行計(jì)算,都是nan
# nan in series
s1 = Series([1, 2, np.nan, 3, 4], index=['A', 'B', 'C', 'D', 'E'])
print(s1)
'''
A 1.0
B 2.0
C NaN
D 3.0
E 4.0
dtype: float64
'''
print(s1.isnull()) # 返回 bool值,是 nan 的話,返回true
'''
A False
B False
C True
D False
E False
dtype: bool
'''
print(s1.notnull()) # 非 nan , 返回true
'''
A True
B True
C False
D True
E True
dtype: bool
'''
# 去掉 有 nan 的索引項(xiàng)
print(s1.dropna())
'''
A 1.0
B 2.0
D 3.0
E 4.0
dtype: float64
'''
# nan in dataframe
df = DataFrame([[1, 2, 3], [np.nan, 5, 6], [7, np.nan, 9], [np.nan, np.nan, np.nan]])
print(df)
'''
0 1 2
0 1.0 2.0 3.0
1 NaN 5.0 6.0
2 7.0 NaN 9.0
3 NaN NaN NaN
'''
print(df.isnull()) # df.notnull() 同理
'''
0 1 2
0 False False False
1 True False False
2 False True False
3 True True True
'''
# 去掉 所有 有 nan 的 行, axis = 0 表示 行方向
df1 = df.dropna(axis=0)
print(df1)
'''
0 1 2
0 1.0 2.0 3.0
'''
# 表示在 列 的方向上。
df1 = df.dropna(axis=1)
print(df1)
'''
mpty DataFrame
Columns: []
Index: [0, 1, 2, 3]
'''
# any 只要有 nan 就會(huì)刪掉。 all 是必須全是nan才刪除
df1 = df.dropna(axis=0, how='any')
print(df1)
'''
0 1 2
0 1.0 2.0 3.0
'''
# any 只要有 nan 就會(huì)刪掉。 all 全部是nan,才會(huì)刪除
df1 = df.dropna(axis=0, how='all')
print(df1)
'''
0 1 2
0 1.0 2.0 3.0
1 NaN 5.0 6.0
2 7.0 NaN 9.0
'''
df2 = DataFrame([[1, 2, 3, np.nan], [2, np.nan, 5, 6], [np.nan, 7, np.nan, 9], [1, np.nan, np.nan, np.nan]])
print(df2)
'''
0 1 2 3
0 1.0 2.0 3.0 NaN
1 2.0 NaN 5.0 6.0
2 NaN 7.0 NaN 9.0
3 1.0 NaN NaN NaN
'''
print(df2.dropna(thresh=None))
'''
Empty DataFrame
Columns: [0, 1, 2, 3]
Index: []
'''
print(df2.dropna(thresh=2)) # thresh 表示一個(gè)范圍,如:每一行的nan > 2,就刪除
'''
0 1 2 3
0 1.0 2.0 3.0 NaN
1 2.0 NaN 5.0 6.0
2 NaN 7.0 NaN 9.0
'''
# 將nan進(jìn)行填充
print(df2.fillna(value=1))
'''
0 1 2 3
0 1.0 2.0 3.0 1.0
1 2.0 1.0 5.0 6.0
2 1.0 7.0 1.0 9.0
3 1.0 1.0 1.0 1.0
'''
# 可以 為指定列 填充不同的 數(shù)值
print(df2.fillna(value={0: 0, 1: 1, 2: 2, 3: 3})) # 指定每一列 填充的數(shù)值
'''
0 1 2 3
0 1.0 2.0 3.0 3.0
1 2.0 1.0 5.0 6.0
2 0.0 7.0 2.0 9.0
3 1.0 1.0 2.0 3.0
'''
# 以下兩個(gè)例子需要說明的是:對(duì)dataframe進(jìn)行dropna,原來的dataframe不會(huì)改變
print(df1.dropna())
'''
0 1 2
0 1.0 2.0 3.0
'''
print(df1)
'''
0 1 2
0 1.0 2.0 3.0
1 NaN 5.0 6.0
2 7.0 NaN 9.0
'''
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python中通過property設(shè)置類屬性的訪問
為了達(dá)到類似C++類的封裝性能,可以使用property來設(shè)置Python類屬性的訪問權(quán)限,本文就介紹一下Python中通過property設(shè)置類屬性的訪問,感興趣的可以了解一下,感興趣的可以了解一下2023-09-09
詳解python requests中的post請(qǐng)求的參數(shù)問題
這篇文章主要介紹了詳解python requests中的post請(qǐng)求的參數(shù)問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
利用Python進(jìn)行時(shí)間序列數(shù)據(jù)分析與可視化的代碼示例
隨著時(shí)間序列數(shù)據(jù)在金融、氣象、生態(tài)等領(lǐng)域的廣泛應(yīng)用,利用Python進(jìn)行時(shí)間序列數(shù)據(jù)分析和可視化已成為重要的技能之一,本文將介紹如何使用Python進(jìn)行時(shí)間序列數(shù)據(jù)分析和可視化,并給出相應(yīng)的代碼示例,需要的朋友可以參考下2023-11-11
Python中Flask-RESTful編寫API接口(小白入門)
這篇文章主要介紹了Python中Flask-RESTful編寫API接口(小白入門),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Tensorflow卷積實(shí)現(xiàn)原理+手寫python代碼實(shí)現(xiàn)卷積教程
這篇文章主要介紹了Tensorflow卷積實(shí)現(xiàn)原理+手寫python代碼實(shí)現(xiàn)卷積教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05
python數(shù)組過濾實(shí)現(xiàn)方法
這篇文章主要介紹了python數(shù)組過濾實(shí)現(xiàn)方法,涉及Python針對(duì)數(shù)組的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
python爬取股票最新數(shù)據(jù)并用excel繪制樹狀圖的示例
這篇文章主要介紹了python爬取股票最新數(shù)據(jù)并用excel繪制樹狀圖的示例,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03

