版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
处理订单数据中的缺失值主讲人:赵若馨重庆市九龙坡职业教育中心问题描述订单数据表如下表表2.1.1所示。表2.1.1订单数据表order_idquantityitem_namechoice_descriptionitem_price11ChipsandFreshTomatoSalsaNaN$2.3911Izze[Clementine]$3.3911NantucketNectar[Apple]$3.3911ChipsandTomatillo-GreenChiliSalsaNaN$2.39...............18341ChickenSaladBowl[FreshTomatoSalsa,[FajitaVegetables,Pinto...$11.2518341ChickenSaladBowl[FreshTomatoSalsa,[FajitaVegetables,Lettu...$8.7518341ChickenSaladBowl[FreshTomatoSalsa,[FajitaVegetables,Pinto...$8.75问题描述现要求根据订单数据表,统计订单表格中,商品描述(choice_description)中缺失值(为“NaN”)的数量,并将其批量替换为“banana”,并输出替换后的数据表以及缺失值的数量。输出结果表2.1.2替换为banana的订单数据表order_idquantityitem_namechoice_descriptionitem_price11ChipsandFreshTomatoSalsabanana$2.3911Izze[Clementine]$3.3911NantucketNectar[Apple]$3.3911ChipsandFreshTomatoSalsabanana$2.39...............18341ChickenSaladBowl[FreshTomatoSalsa,[FajitaVegetables,Pinto...$11.2518341ChickenSaladBowl[FreshTomatoSalsa,[FajitaVegetables,Lettu...$8.7518341ChickenSaladBowl[FreshTomatoSalsa,[FajitaVegetables,Pinto...$8.75【choice_description】为空值的数量为:1246题前思考表2.1.3问题分析问题描述问题解答怎样读取csv文件中的数据?如何统计数据中的缺失值的数量?
怎样将缺失值替换为指定数据?read_csv()
isnull(),query(),shapefillna()
统计缺失值数量,并替换为指定数据。importpandasaspdorder_data=pd.read_csv(r"D:\pydata\项目二\订单数据表.csv",encoding='utf-8')①num=order_data.query('choice_description.isnull()',engine='python').shape[0]②order_data.fillna({'choice_description':'banana'},inplace=True)③print(order_data)print("【choice_description】为空值的数量为:",num)程序代码程序代码importpandasaspdorder_data=pd.read_csv(r"D:\pydata\项目二\订单数据表.csv",encoding='utf-8')①num=order_data.query('choice_description.isnull()',engine='python').shape[0]order_data.fillna({'choice_description':'banana'},inplace=True)print(order_data)print("【choice_description】为空值的数量为:",num)read_csv()读取外部csv文件中的数据并将数据以数据框的形式保存到变量order_data。程序代码表2.1.4order_data内容order_idquantityitem_namechoice_descriptionitem_price11ChipsandFreshTomatoSalsaNaN$2.3911Izze[Clementine]$3.3911NantucketNectar[Apple]$3.3911ChipsandTomatillo-GreenChiliSalsaNaN$2.39...............18341ChickenSaladBowl[FreshTomatoSalsa,[FajitaVegetables,Pinto...$11.2518341ChickenSaladBowl[FreshTomatoSalsa,[FajitaVegetables,Lettu...$8.7518341ChickenSaladBowl[FreshTomatoSalsa,[FajitaVegetables,Pinto...$8.75程序代码importpandasaspdorder_data=pd.read_csv(r"D:\pydata\项目二\订单数据表.csv",encoding='utf-8')①num=order_data.query('choice_description.isnull()',engine='python').shape[0]order_data.fillna({'choice_description':'banana'},inplace=True)print(order_data)print("【choice_description】为空值的数量为:",num)计算'choice_description'列中空值的个数。将值存入变量num②1246程序代码importpandasaspdorder_data=pd.read_csv(r"D:\pydata\项目二\订单数据表.csv",encoding='utf-8')①num=order_data.query('choice_description.isnull()',engine='python').shape[0]order_data.fillna({'choice_description':'banana'},inplace=True)print(order_data)print("【choice_description】为空值的数量为:",num)isnull()判断”choice_description”列的值是否为空;得到一个与order_data.choice_description结构相同的逻辑值序列,若choice_description中某值为空,则该序列对应位置的值为True,反之为False②程序代码choice_description.isnull()调用结果如下0True1False2False3True4False...4617False4618False4619False4620False4621FalseName:choice_description,Length:4622,dtype:bool程序代码importpandasaspdorder_data=pd.read_csv(r"D:\pydata\项目二\订单数据表.csv",encoding='utf-8')①num=order_data.query('choice_description.isnull()',engine='python').shape[0]order_data.fillna({'choice_description':'banana'},inplace=True)print(order_data)print("【choice_description】为空值的数量为:",num)query()'会选择上述结果中值为True的数据行构成一个数据框,即选择'choice_description'列值为空的行。②程序代码order_idquantityitem_namechoice_descriptionitem_price11ChipsandFreshTomatoSalsaNaN$2.3911ChipsandTomatillo-GreenChiliSalsaNaN$2.3931SideofChipsNaN$1.6951ChipsandGuacamoleNaN$4.45...............18281ChipsandGuacamoleNaN$4.4518311ChipsNaN$2.1518311BottledWaterNaN$1.5018321ChipsandGuacamoleNaN$4.45表2.1.5'choice_description'列值为空的行程序代码importpandasaspdorder_data=pd.read_csv(r"D:\pydata\项目二\订单数据表.csv",encoding='utf-8')①num=order_data.query('choice_description.isnull()',engine='python').shape[0]order_data.fillna({'choice_description':'banana'},inplace=True)print(order_data)print("【choice_description】为空值的数量为:",num)shape[0]表示数据框的行数,也就是'choice_description'列中空值的个数。shape表示数据框的形状,此处的数据框是一个二维表,shape就表示它的行列数,shape[0]表示行数,shape[1]表示列数②程序代码importpandasaspdorder_data=pd.read_csv(r"D:\pydata\项目二\订单数据表.csv",encoding='utf-8')①num=order_data.query('choice_description.isnull()',engine='python').shape[0]order_data.fillna({'choice_description':'banana'},inplace=True)③print(order_data)print("【choice_description】为空值的数量为:",num)②
使用fillna()方法将“choice_description”列的缺失值替换为“banana”;inplace=True,表示在源数据上进行修改。程序代码替换后的数据表order_idquantityitem_namechoice_descriptionitem_price11ChipsandFreshTomatoSalsabanana$2.3911Izze[Clementine]$3.3911NantucketNectar[Apple]$3.3911ChipsandFreshTomatoSalsabanana$2.39...............18341ChickenSaladBowl[FreshTomatoSalsa,[FajitaVegetables,Pinto...$11.2518341ChickenSaladBowl[FreshTomatoSalsa,[FajitaVegetables,Lettu...$8.7518341ChickenSaladBowl[FreshTomatoSalsa,[FajitaVegetables,Pinto...$8.75任务小结1.isnull(),找出含有空值(NaN)的数据行2.query(),查询DataFrame的列3.fillna(),填充缺失数据4.replace(),替换为指定值
order_data.fillna({'choice_description':'banana'},inplace=True)
order_data.replace(np.NaN,'bana
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 药事管理法规考试题库及答案
- 药品管理法题库带答案
- 营养与膳食考试模拟试卷(含答案)
- 行业资格考试题库及参考答案
- 行测专项题库及答案
- 警校考编模拟试题库带答案
- 诊断学病例题库及答案
- 起重机司机(限桥式起重机)操作证考试题库及答案
- 辽宁省沈阳市和平区2026年注册土木工程师考模拟试题库及参考答案
- 退役士兵面试题库带答案
- 2019年消防继续教育试题汇 总及答案
- 低钾血症诊治与管理专家共识(2026)解读课件
- 2026年长沙幼儿师范高等专科学校单招职业技能测试题库及参考答案详解一套
- 2026年四川省普通高中信息技术学业水平合格性考试试卷及答案
- 2026年中国出口信用保险公司招聘笔试大纲及备考指南
- 2026年深泽县辅警招聘考试备考题库必考题
- 江苏省南京市玄武区2025-2026学年九年级上学期期中历史试卷(含答案)
- 上海交易集团秋招面笔试题及答案
- 整县屋顶分布式光伏项目绿色施工方案
- 手术室精益管理汇报
- T/CAR 10-2022食品速冻装置隧道式速冻装置
评论
0/150
提交评论