列处理——寻找并处理非法值
2016-01-05 20:17
344 查看
#input is a list named legislators, the first two elements looks like this: #[['Bassett', 'Richard', '1745-04-02', 'M', 'sen', 'DE', 'Anti-Administration'], ['Bland', 'Theodorick', '1742-03-21', '', 'rep', 'VA', '']] #Bassett Richard is the name #1745-04-02 is bithdate #M is gender
对性别列做处理。
单独取出该字段
genders_list = [] for rows in legislators: genders_list.append(rows[3])
得到该字段的所有取值
# Converting to a set, so we get the unique values unique_genders = set(genders_list) # We can't index sets, so we need to convert back into a list first. unique_genders_list = list(unique_genders) print(unique_genders_list) # 输出为['', 'M', 'F']
看出,除了正常的M,F,还会有空值。
pandas获取方法:
#recent_grads['Major'].value_counts()是series类型 majors = recent_grads['Major'].value_counts().index
处理非法值
for row in legislators: if row[3]=='': row[3]='M'
相关文章推荐
- Activity后台运行一段时间回来crash问题的分析与解决
- highcharts与ajax的应用
- Linux内核构建系统之二
- Android 4.4环境搭建——Android SDK下载与安装
- CocoaPods安装和使用及问题:Setting up CocoaPods master repo
- 七个对我最好的职业建议 Nicholas C. Zakas
- Python,Jupyter Notebook,IPython快速安装教程
- 查询表结构
- Python,IPython,qtconsole,Notebook,Jupyter快速安装教程
- Linux内核构建系统之一
- 面向对象的设计模式(十一),装饰模式
- 一个字符串排序的程序
- CodeForces 441A Valera and Antique Items
- java代理机制
- bzoj4196[Noi2015]软件包管理器
- POJ1003_Hangover
- ajaxfileupload上传文件出现SyntaxError:unexpected token <错误
- HTML article和section
- Unable to load native-hadoop library for your platform(已解决)
- 企业培训的多角度开发