【问题标题】:How to group rows into a list from csv without using pandas如何在不使用熊猫的情况下将行从csv分组到列表中
【发布时间】:2021-12-12 11:17:59
【问题描述】:

我有一个看起来像这样的 csv,

#cars.csv
Bugatti, Veyron 
Bugatti, Chiron
VW, Golf
VW, Passat
VW, Polo
VW, Caddy
Opel, Insignia

我想像这样创建两个单独的列表:

brands = ["Bugatti","VW","Opel"]

models = [["Veyron","Chiron"],
          ["Golf","Passat","Polo","Caddy"],
          ["Insignia"]]

没有熊猫有什么可能的方法吗?我在导入熊猫时遇到了麻烦,所以我正在寻找一种替代方法。 感谢所有帮助!

【问题讨论】:

  • 你为什么不问如何解决 pandas 安装问题呢?安装pandas的时候有什么错误提示吗?

标签: python list csv group-by


【解决方案1】:

您可以查看convtools 库,它提供了许多数据处理原语

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


# prepare converter (this is where code generation happens, so
# it's better to store the converter somewhere for further reuse)
converter = (
    c.group_by(c.item("brand"))
    .aggregate(
        {
            "brand": c.item("brand"),
            "models": c.ReduceFuncs.ArrayDistinct(c.item("model")),
        }
    )
    .pipe(
        {
            "brands": c.iter(c.item("brand")).as_type(list),
            "models": c.iter(c.item("models")).as_type(list),
        }
    )
    .gen_converter()
)

# read the csv
rows = Table.from_csv(
    "cars.csv",
    header=["brand", "model"],
    dialect=Table.csv_dialect(skipinitialspace=True),
).into_iter_rows(dict)
# process the rows
assert converter(rows) == {
    "brands": ["Bugatti", "VW", "Opel"],
    "models": [
        ["Veyron ", "Chiron"],
        ["Golf", "Passat", "Polo", "Caddy"],
        ["Insignia"],
    ],
}

【讨论】:

    【解决方案2】:
    from collections import defaultdict
    
    def parse(file, delimiter=', '):
        with open(file) as f:
            result = defaultdict(list)
            for line in f.readlines():
                brand, model = line.split(delimiter)
                result[brand].append(model)
            return result
    
    result = parse('cars.csv')
    
    # fit the special data format
    brands = list(result.keys())
    models = [result[key] for key in brands]
    

    如果你有任何问题,请告诉我。

    【讨论】:

      【解决方案3】:

      给你:

      import csv
      file = open("cars.csv")
      csvreader = csv.reader(file)
      rows = [i for i in csvreader]
      brands = []
      models = []
      for brand, model in rows:
          if brand in brands:
              models[brands.index(brand)].append(model)
          else:
              brands.append(brand)
              models.append([model])
      file.close()
      

      【讨论】:

      • 非常感谢!你能向我解释一下 rows = [i for i in csvreader] 的用途吗?我是一个非常新的初学者
      • 它获取每行的列列表。例如:[[“布加迪”、“威龙”]、[“布加迪”、“凯龙”]..]
      猜你喜欢
      • 2021-06-08
      • 2022-12-24
      • 2021-06-24
      • 2021-08-21
      • 2021-04-27
      • 2019-07-04
      • 2021-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多