Pandas对缺失数据和文本数据的处理

我们知道Pandas中最常用的两种数据类型就是Series 和DataFrame,因此主要学习者两种数据类型的缺失数据的处理方法

拿到一份数据,首先查看数据完整性,了解缺失信息;
然后针对缺失的数据,是要进行填充?还是删除?还是插值?选择合适的方式对拿到的数据进行数据预处理。

1. 了解缺失信息:主要就是用isna和notna方法,对于isna方法,如果为缺失值,则返回True,如果不为缺失值,则返回False;notna方法则反之

import numpy as np
import pandas as pd
#首选读取一份数据
df = pd.read_csv('E:/data/table_missing.csv')
#这样子读出来的数据结构是DataFrame
df.head()
SchoolClassIDGenderAddressHeightWeightMathPhysics
0S_1C_1NaNMstreet_1173NaN34.0A+
1S_1C_1NaNFstreet_2192NaN32.5B+
2S_1C_11103.0Mstreet_2186NaN87.2B+
3S_1NaNNaNFstreet_216781.080.4NaN
4S_1C_11105.0NaNstreet_415964.084.8A-
#然后通过isna方法或者notna方法来查看缺失值
df['Physics'].isna().head()
0    False
1    False
2    False
3     True
4    False
Name: Physics, dtype: bool

有时候对于一份数据,我们并不关心某一个缺失值,而是关心某个需要分析的属性值的数据完整性。如果该属性数据缺失较少,
可以考虑用合适的插值方法来对数据进行拟合(模拟);如果数据缺失较多,或许该数据数据就不适合应用到我们的分析当中。
可以利用如下方法来了解拿到手的数据的一个完整性情况:

#计算每一列有多少个缺失值
df.isna().sum()
School      0
Class       4
ID          6
Gender      7
Address     0
Height      0
Weight     13
Math        5
Physics     4
dtype: int64
#也可以通过info函数来查看每一列的数据缺失情况
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 35 entries, 0 to 34
Data columns (total 9 columns):
 #   Column   Non-Null Count  Dtype  
---  ------   --------------  -----  
 0   School   35 non-null     object 
 1   Class    31 non-null     object 
 2   ID       29 non-null     float64
 3   Gender   28 non-null     object 
 4   Address  35 non-null     object 
 5   Height   35 non-null     int64  
 6   Weight   22 non-null     float64
 7   Math     30 non-null     float64
 8   Physics  31 non-null     object 
dtypes: float64(3), int64(1), object(5)
memory usage: 2.6+ KB

2.处理缺失值

2.1 填充

主要用到的方法是:DataFrame.fillna(value = None,method = None,axis = None, inplace = False,limit=None, downcast=None, **kwargs)
其中,
value是要用去填充缺失值的值;

# 1.直接用missing去填充所有的缺失值
df.fillna('missing').head()
SchoolClassIDGenderAddressHeightWeightMathPhysics
0S_1C_11103.0Mstreet_117381.034.0A+
1S_1C_11103.0Fstreet_219281.032.5B+
2S_1C_11103.0Mstreet_218681.087.2B+
3S_1C_11105.0Fstreet_216781.080.4A-
4S_1C_11105.0Mstreet_415964.084.8A-
# 2.用缺失值上面的值替换缺失值
df.fillna(method= 'ffill').head()
SchoolClassIDGenderAddressHeightWeightMathPhysics
0S_1C_1NaNMstreet_1173NaN34.0A+
1S_1C_1NaNFstreet_2192NaN32.5B+
2S_1C_11103.0Mstreet_2186NaN87.2B+
3S_1C_11103.0Fstreet_216781.080.4B+
4S_1C_11105.0Fstreet_415964.084.8A-
# 3.用缺失值下面的值替换缺失值
df.fillna(method='backfill').head()
SchoolClassIDGenderAddressHeightWeightMathPhysics
0S_1C_11103.0Mstreet_117381.034.0A+
1S_1C_11103.0Fstreet_219281.032.5B+
2S_1C_11103.0Mstreet_218681.087.2B+
3S_1C_11105.0Fstreet_216781.080.4A-
4S_1C_11105.0Mstreet_415964.084.8A-
2.2 剔除

主要用的是函数:DataFrame.dropna(axis=0, how=‘any’, thresh=None, subset=None, inplace=False)
删除含有空值的行或者列

# 1.删除行,只要哪行有空值就删掉哪行;
df.dropna().head()
SchoolClassIDGenderAddressHeightWeightMathPhysics
5S_1C_21201.0Mstreet_515968.097.0A-
6S_1C_21202.0Fstreet_417694.063.5B-
12S_1C_31303.0Mstreet_718882.049.7B
17S_2C_12103.0Mstreet_415761.052.5B-
21S_2C_22202.0Fstreet_719477.068.5B+
# 2. 删除列,只要哪一列有空值,就删掉哪一列
df.dropna(axis= 1).head()
SchoolAddressHeight
0S_1street_1173
1S_1street_2192
2S_1street_2186
3S_1street_2167
4S_1street_4159
# 3.某一行所有值全为缺失值才删除
df.dropna(how= 'all').head()
SchoolClassIDGenderAddressHeightWeightMathPhysics
0S_1C_1NaNMstreet_1173NaN34.0A+
1S_1C_1NaNFstreet_2192NaN32.5B+
2S_1C_11103.0Mstreet_2186NaN87.2B+
3S_1NaNNaNFstreet_216781.080.4NaN
4S_1C_11105.0NaNstreet_415964.084.8A-
# 4.某一行至少出现过2个缺失值才删除
df.dropna(thresh=2).head()
SchoolClassIDGenderAddressHeightWeightMathPhysics
0S_1C_1NaNMstreet_1173NaN34.0A+
1S_1C_1NaNFstreet_2192NaN32.5B+
2S_1C_11103.0Mstreet_2186NaN87.2B+
3S_1NaNNaNFstreet_216781.080.4NaN
4S_1C_11105.0NaNstreet_415964.084.8A-
2.3插值
2.3.1 线性插值
s = pd.Series([1,10,15,-5,-2,np.nan,np.nan,28])
s.plot()

在这里插入图片描述

s.interpolate().plot()

在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值