【问题标题】:How can I return the most reoccuring value in the given csv list?如何返回给定 csv 列表中最常出现的值?
【发布时间】:2022-01-12 07:12:48
【问题描述】:

我之前问过这个问题,但没有作为函数提供答案。我试过输入一个功能,但它没有用,所以我再问一次:)

这是一个我必须分析的示例 CSV 文件

1,8dac2b,ewmzr,jewelry,phone0,9759243157894736,us,69.166.231.58,vasstdc27m7nks3
2,668d39,aeqok,furniture,phone1,9759243157894736,jp,50.201.125.84,jmqlhflrzwuay9c
3,622r49,arqek,vehicle,phone2,9759544365415694736,az,53.001.135.54,weqlhrerreuert6f
4,6444t43,rrdwk,vehicle,phone9,9759543263245434353,au,54.241.234.64,weqqyqtqwrtert6f

我尝试使用此函数def popvote(list) 来返回csv 列表中每个列表的第四行 中最受欢迎的内容,在上面的示例中为vehicle

下面的解释

这是我目前所拥有的

def popvote(list):
    for x in list:
        g = list(x)
        if x = max(g[x]):
           return x

但是,这并没有真正起作用..我应该更改什么以确保它起作用??

注意:答案应作为集合返回

说明:所以我试图根据下面 (** xxxx **) 中指示的内容返回列表中重复次数最多的值

1,8dac2b,ewmzr,**jewelry**,phone0,9759243157894736,us,69.166.231.58,vasstdc27m7nks3
2,668d39,aeqok,**furniture**,phone1,9759243157894736,jp,50.201.125.84,jmqlhflrzwuay9c
3,622r49,arqek,**vehicle**,phone2,9759544365415694736,az,53.001.135.54,weqlhrerreuert6f    
4,6444t43,rrdwk,**vehicle**,phone9,9759543263245434353,au,54.241.234.64,weqqyqtqwrtert6f

所以在这种情况下,车辆应该是输出。

【问题讨论】:

  • 这是 pandas 的一个很好的用例:pd.read_csv("filename.csv", header=None)[3].mode() 在第四列中给出了一系列最常见的条目(在这种情况下是唯一的)。

标签: python list function csv


【解决方案1】:

原始python方法,使用collections.Counter

import csv
from collections import Counter

def read_categories():
    with open("tmp.csv", "r") as f:
        reader = csv.reader(f)
        for row in reader:
            yield row[3]

counter = Counter(read_categories())
counter.most_common(n=1)
# [('vehicle', 2)]

仅限原始 python:

import csv

value_to_count = {}
with open("tmp.csv", "r") as f:
    reader = csv.reader(f)
    for row in reader:
        category = row[3]
        if category in value_to_count:
            value_to_count[category] += 1
        else:
            value_to_count[category] = 1

# sorted list of counts and values
count_to_value = sorted((v, k) for k, v in value_to_count.items())

if count_to_value:
    print("most common", count_to_value[-1])
    # most common (2, 'vehicle')

如果你觉得convtools 有用,那么:

from convtools import conversion as c
from convtools.contrib.tables import Table

rows = Table.from_csv("tmp.csv", header=False).into_iter_rows(tuple)

# this is where code generation happens, it makes sense to store
# the converter in a separate variable for further reuse
converter = c.aggregate(c.ReduceFuncs.Mode(c.item(3))).gen_converter()
converter(rows)
# "vehicle"

【讨论】:

  • 有没有办法不使用集合和计数器。使用 import csv 很好,但我真的想避免使用集合和计数器。这可能吗?
  • 如果是这样,你能告诉我怎么做吗?我真的不明白:)
  • @jordanparker 刚刚编辑了答案,见上文
  • 非常感谢你:)。只是最后一个问题。如何将您编辑的代码放在一个函数下,例如,在我原来的 FUNCTION def popvote(list) 中。因为我试着把它放在我的函数下但它不起作用。
  • def popvote(filename): 然后粘贴你需要的所有行
【解决方案2】:

正如评论所指出的,您可以使用df.mode() 和类型转换来设置结果。

df = pd.read_csv("filename.csv", header=None)
set(df[3].mode())

Out: {'vehicle'}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 2017-03-18
    相关资源
    最近更新 更多