pandas实战日志20211114——数据预处理

1、数据预处理——查看空值

# 查看空值的方法

shop.info()  # 查看表结构,通过各字段数据类型及数据量

print(shop.isnull().sum()) # 查看各字段空值数量
# 输出结果1
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2000 entries, 0 to 1999
Data columns (total 10 columns):
 #   Column       Non-Null Count  Dtype  
---  ------       --------------  -----  
 0   shop_id      2000 non-null   int64  
 1   city_name    2000 non-null   object 
 2   location_id  2000 non-null   int64  
 3   per_pay      2000 non-null   int64  
 4   score        1709 non-null   float64
 5   comment_cnt  1709 non-null   float64
 6   shop_level   2000 non-null   int64  
 7   cate_1_name  2000 non-null   object 
 8   cate_2_name  2000 non-null   object 
 9   cate_3_name  1415 non-null   object 
dtypes: float64(2), int64(4), object(4)
memory usage: 156.4+ KB

# 输出结果2:
shop_id          0
city_name        0
location_id      0
per_pay          0
score          291
comment_cnt    291
shop_level       0
cate_1_name      0
cate_2_name      0
cate_3_name    585
dtype: int64

空值处理方法:

a. 删除空值 df.dropna()

b. 填充空值 df.fillna()

2、数据预处理——查看重复值(经检验数据无重复值)

# 使用duplicated查看重复值:
# DataFrame.duplicated(self, subset=None, keep='first')
# subset:只考虑标识重复项的某些列,默认情况下使用所有列
# keep : {‘first’, ‘last’, False}, default ‘first’,标记重复项

print(shop[shop.duplicated(subset='shop_id')]) #查看商家id重复的数据

删除重复值方法:df.drop_duplicates()

3、数据预处理——查看异常值

# 主要使用排序方法sort_values()查看异常值

print(shop.sort_values(by='per_pay',ascending=False)) #查看人均消费分布(1~20)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值