您当前的位置: 首页 > 
  • 4浏览

    0关注

    214博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Panads(四):数据清洗——对缺失值的处理

不愿透露姓名の网友 发布时间:2020-06-21 16:23:02 ,浏览量:4

文章目录
  • 一、处理缺失值的四个函数
  • 二、使用
    • 1.1 数据样子
    • 1.2 处理

一、处理缺失值的四个函数
  • ①isnull函数:检测是否是空值,可用于df和series
  • ②notnull函数:检测是否是空值,可用于df和series
  • ③dropna函数:丢弃、删除缺失值
参数介绍axis删除行还是列,{0 or ‘index’, 1 or ‘columns’}, default 0how如果等于any则任何值为空都删除,如果等于all则所有值都为空才删除inplace如果为True则修改当前df,否则返回新的df
  • ④fillna函数:填充空值
参数介绍value用于填充的值,可以是单个值,或者字典(key是列名,value是值)method等于ffill使用前一个不为空的值填充forword fill;等于bfill使用后一个不为空的值填充backword fillaxis按行还是列填充,{0 or ‘index’, 1 or ‘columns’}inplace如果为True则修改当前df,否则返回新的df 二、使用 1.1 数据样子

未处理前 在这里插入图片描述 要清洗后的样式 在这里插入图片描述

1.2 处理

①读取表格,跳过两行

import pandas as pd
df = pd.read_excel('./student.xlsx', skiprows=2)
print(df.head())

在这里插入图片描述 ②检测缺失值

print(df.isnull())
# print(df.notnull())

在这里插入图片描述 ③删除全是空值的列

 # axis可以写1,how将全部为空,inplace是本表应用
df.dropna(axis='columns', how='all', inplace=True) 
print(df)

在这里插入图片描述 ④删除全是空值得行

 # axis可以写0,how将全部为空,inplace是本表应用
df.dropna(axis='index', how='all', inplace=True) 
print(df)

在这里插入图片描述 ⑤将分数列为空的填充为0

# df.fillna({'分数':0})#效果同下
df.loc[:, '分数'] = df['分数'].fillna(0)
print(df)

在这里插入图片描述 ⑥将姓名的缺失值填充

df.loc[:, '姓名'] = df['姓名'].fillna(method="ffill")#按照上一行自动填充
print(df)

在这里插入图片描述 ⑦将清洗的表格保存

df.to_excel("./new_student.xlsx", index=False)
关注
打赏
1657102503
查看更多评论
立即登录/注册

微信扫码登录

0.0836s