码迷,mamicode.com
首页 > 其他好文 > 详细

列处理——寻找并处理非法值

时间:2016-01-05 22:16:16      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

#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,还会有空值。

  • 处理非法值
for row in legislators:
    if row[3]==‘‘:
        row[3]=M

 

列处理——寻找并处理非法值

标签:

原文地址:http://www.cnblogs.com/arsh/p/5103629.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!