吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3372|回复: 1
收起左侧

[会员申请] 会员申请ID:527597340

[复制链接]
吾爱游客  发表于 2020-2-25 10:06
1.申请ID:527597340
2.个人邮箱:527597340@qq.com
3.原创技术文章:CSDN 戴翔的技术博客  《计算机基础进制转换(二进制、八进制、十进制、十六进制)》,《数据清洗--DataFrame中的空值处理》等
[color=rgba(0, 0, 0, 0.75)]CSDN博客专家,秦淮区疾控中心托管社会公益组织指南针工作室志愿者,创业公司研发中心负责人,在校大学生。第二届大学生微信小程序应用开发大赛全国三等奖项目第一作者,微信小程序《约车吗》、《庆云慈善会》、《锋云速检》项目总工程师。



数据清洗是一项复杂且繁琐的工作,同时也是整个数据分析过程中最为重要的环节。在python中空值被显示为NaN。首先,我们要构造一个包含NaN的DataFrame对象。
[Python] [color=rgb(51, 102, 153) !important]纯文本查看 [color=rgb(51, 102, 153) !important]复制代码
[color=white !important]?
0102030405060708091011
>>> import numpy as np>>> import pandas as pd>>> from pandas import Series,DataFrame>>> from numpy import nan as NaN>>> data = DataFrame([[12,'man','13865626962'],[19,'woman',NaN],[17,NaN,NaN],[NaN,NaN,NaN]],columns=['age','sex','phone'])>>> data    age    sex        phone0  12.0    man  138656269621  19.0  woman          NaN2  17.0    NaN          NaN3   NaN    NaN          NaN
删除NaN删除NaN所在的行删除表中全部为NaN的行
[Python] [color=rgb(51, 102, 153) !important]纯文本查看 [color=rgb(51, 102, 153) !important]复制代码
[color=white !important]?
12345
>>> data.dropna(axis=0, how='all')    age    sex        phone0  12.0    man  138656269621  19.0  woman          NaN2  17.0    NaN          NaN
删除表中任何含有NaN的行
[Python] [color=rgb(51, 102, 153) !important]纯文本查看 [color=rgb(51, 102, 153) !important]复制代码
[color=white !important]?
123
>>> data.dropna(axis=0, how='any')    age  sex        phone0  12.0  man  13865626962
删除NaN所在的列删除表中全部为NaN的列
[Python] [color=rgb(51, 102, 153) !important]纯文本查看 [color=rgb(51, 102, 153) !important]复制代码
[color=white !important]?
123456
>>> data.dropna(axis=1, how='all')    age    sex        phone0  12.0    man  138656269621  19.0  woman          NaN2  17.0    NaN          NaN3   NaN    NaN          NaN
删除表中任何含有NaN的列
[Python] [color=rgb(51, 102, 153) !important]纯文本查看 [color=rgb(51, 102, 153) !important]复制代码
[color=white !important]?
1234
>>> data.dropna(axis=1, how='any')Empty DataFrameColumns: []Index: [0, 1, 2, 3]
注意:axis 就是”轴,数轴“的意思,对应多维数组里的”维“。此处作者的例子是二维数组,所以,axis的值对应表示:0轴(行),1轴(列)。填充NaN如果不想过滤(去除)数据,我们可以选择使用fillna()方法填充NaN,这里,作者使用数值’0’替代NaN,来填充DataFrame。
[Python] [color=rgb(51, 102, 153) !important]纯文本查看 [color=rgb(51, 102, 153) !important]复制代码
[color=white !important]?
123456
>>> data.fillna(0)    age    sex        phone0  12.0    man  138656269621  19.0  woman            02  17.0      0            03   0.0      0            0
我们还可以通过字典来填充,以实现对不同的列填充不同的值。
[Python] [color=rgb(51, 102, 153) !important]纯文本查看 [color=rgb(51, 102, 153) !important]复制代码
[color=white !important]?
123456
>>> data.fillna({'sex':233,'phone':666})    age    sex        phone0  12.0    man  138656269621  19.0  woman          6662  17.0    233          6663   NaN    233          666

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Hmily 发表于 2020-2-25 17:05
格式乱七八糟的完全看不懂是啥。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-3-29 02:10

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表