简体   繁体   English

用于csv文件和更新空白单元格的collections.Counter

[英]collections.Counter for csv file and updating blank cells

I am importing data from a csv file; 我正在从csv文件导入数据; and trying to determine a user type. 并尝试确定用户类型。 In specific, I am using the python module collections. 具体来说,我正在使用python模块集合。

My data set has blank fields thus when I execute the following script type_of_users=user_data.most_common() I get the following result: 我的数据集具有空白字段,因此当我执行以下脚本type_of_users=user_data.most_common() ,将得到以下结果:

The type of users are : 用户类型为:

[('Subscriber', 269149), ('Customer', 30159), ('', 692)]

Is there a way to update the ('',692) with ('Unknown User Type',692) ? 有没有一种方法可以用('Unknown User Type',692)更新('',692) ('Unknown User Type',692)

You could use a list comprehension on the results of .most_common() after reading the file: 读取文件后,可以对.most_common()的结果使用列表.most_common()

>>> type_of_users = [('Subscriber', 269149), ('Customer', 30159), ('', 692)]

>>> type_of_users = [(i, j) if i else ('Unknown User Type', j)
...                  for i, j in type_of_users]

>>> type_of_users
[('Subscriber', 269149), ('Customer', 30159), ('Unknown User Type', 692)]

This takes advantage of the property that empty sequences ('') are considered False while non-empty strings are considered True. 这利用了以下属性:将空序列('')视为False,将非空字符串视为True。

Note that there are a number of other objects that will also evaluate in this way, so if you need to be more explicit, you should use if i != '' . 请注意,还有许多其他对象也将以这种方式求值,因此,如果需要更明确地说明,则应使用if i != ''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM