关于pandas:Pandas缺失值处理超强讲解

7次阅读

共计 3068 个字符,预计需要花费 8 分钟才能阅读完成。

本篇详解 pandas 中缺失值(Missing data handling)解决罕用操作。

缺失值解决罕用于数据分析数据荡涤阶段;

Pandas 中将如下类型定义为缺失值:

NaN:‘’,‘#N/A’,‘#N/A N/A’,‘#NA’,‘-1.#IND’,‘-1.#QNAN’,‘-NaN’,‘-nan’,‘1.#IND’,‘1.#QNAN’,‘<NA>’,‘N/A’,‘NA’,‘NULL’,‘NaN’,‘n/a’,‘nan’,‘null’,None

1、pandas 中缺失值注意事项

pandas 和 numpy 中 任意两个缺失值不相等(np.nan != np.nan)

下图中两个 NaN 不相等:

In [224]: df1.iloc[3:,0].values# 取出 'one' 列中的 NaN
Out[224]: array([nan])

In [225]: df1.iloc[2:3,1].values# 取出 'two' 列中的 NaN
Out[225]: array([nan])

In [226]: df1.iloc[3:,0].values == df1.iloc[2:3,1].values# 两个 NaN 值不相等
Out[226]: array([False])

pandas 读取文件时 那些值被视为缺失值

NaN:‘’,‘#N/A’,‘#N/A N/A’,‘#NA’,‘-1.#IND’,‘-1.#QNAN’,‘-NaN’,‘-nan’,‘1.#IND’,‘1.#QNAN’,‘<NA>’,‘N/A’,‘NA’,‘NULL’,‘NaN’,‘n/a’,‘nan’,‘null’,None

2、pandas 缺失值操作

pandas.DataFrame 中 判断那些值是缺失值:isna 办法

# 定义一个试验 DataFrame
In [47]: d = {'one': pd.Series([1., 2., 3.], index=['a', 'b', 'c']),'two': pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}

In [48]: df = pd.DataFrame(d)
In [49]: df
Out[49]:
   one  two
a  1.0  1.0
b  2.0  2.0
c  3.0  3.0
d  NaN  4.0

In [120]: df.isna()# 返回形态一样的 bool 值填充 DataFrame
Out[120]:
     one    two
a  False  False
b  False  False
c  False  False
d   True  False

pandas.DataFrame 中 删除蕴含缺失值的行:dropna(axis=0)

In [67]: df
Out[67]:
   one  two
a  1.0  1.0
b  2.0  2.0
c  3.0  3.0
d  NaN  4.0

In [68]: df.dropna()# 默认 axis=0
Out[68]:
   one  two
a  1.0  1.0
b  2.0  2.0
c  3.0  3.0

pandas.DataFrame 中 删除蕴含缺失值的列:dropna(axis=1)

In [72]: df.dropna(axis=1)
Out[72]:
   two
a  1.0
b  2.0
c  3.0
d  4.0

pandas.DataFrame 中 删除蕴含缺失值的列和行:dropna(how=’any’)

In [97]: df['three']=np.nan# 新增一列全为 NaN
In [98]: df
Out[98]:
   one  two  three
a  1.0  1.0    NaN
b  2.0  2.0    NaN
c  3.0  3.0    NaN
d  NaN  4.0    NaN

In [99]: df.dropna(how='any')
Out[99]:
Empty DataFrame# 全删除了
Columns: [one, two, three]
Index: []

pandas.DataFrame 中 删除全是缺失值的行:dropna(axis=0,how=’all’)

In [101]: df.dropna(axis=0,how='all')
Out[101]:
   one  two  three
a  1.0  1.0    NaN
b  2.0  2.0    NaN
c  3.0  3.0    NaN
d  NaN  4.0    NaN

pandas.DataFrame 中 删除全是缺失值的列:dropna(axis=1,how=’all’)

In [102]: df.dropna(axis=1,how='all')
Out[102]:
   one  two
a  1.0  1.0
b  2.0  2.0
c  3.0  3.0
d  NaN  4.0

pandas.DataFrame 中 应用某个值填充缺失值:fillna(某个值)

In [103]: df.fillna(666)# 应用 666 填充
Out[103]:
     one  two  three
a    1.0  1.0  666.0
b    2.0  2.0  666.0
c    3.0  3.0  666.0
d  666.0  4.0  666.0

pandas.DataFrame 中 应用前一列的值填充缺失值:fillna(axis=1,method=’ffill’)

# 后一列填充为 fillna(axis=1,method=bfill')
In [109]: df.fillna(axis=1,method='ffill')
Out[109]:
   one  two  three
a  1.0  1.0    1.0
b  2.0  2.0    2.0
c  3.0  3.0    3.0
d  NaN  4.0    4.0

pandas.DataFrame 中 应用前一行的值填充缺失值:fillna(axis=0,method=’ffill’)

# 后一行填充为 fillna(axis=1,method=bfill')
In [110]: df.fillna(method='ffill')
Out[110]:
   one  two  three
a  1.0  1.0    NaN
b  2.0  2.0    NaN
c  3.0  3.0    NaN
d  3.0  4.0    NaN

pandas.DataFrame 中 应用字典传值填充指定列的缺失值

In [112]: df.fillna({'one':666})# 填充 one 列的 NaN 值
Out[112]:
     one  two  three
a    1.0  1.0    NaN
b    2.0  2.0    NaN
c    3.0  3.0    NaN
d  666.0  4.0    NaN

In [113]: df.fillna({'three':666})
Out[113]:
   one  two  three
a  1.0  1.0  666.0
b  2.0  2.0  666.0
c  3.0  3.0  666.0
d  NaN  4.0  666.0

以上就是本次分享的所有内容,如果你感觉文章还不错,欢送关注公众号:Python 编程学习圈,每日干货分享,发送“J”还可支付大量学习材料。或是返回编程学习网,理解更多编程技术常识。

正文完
 0