Home > OS >  How to group rows into a list from csv without using pandas
How to group rows into a list from csv without using pandas

Time:10-28

I have a csv that looks like this,

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

I want to create two separate lists like this:

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

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

Is there any possible way to do it without pandas? I have trouble importing pandas so I'm looking for an alternate way to do it. All help is appreciated!

CodePudding user response:

Here you go:

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()

CodePudding user response:

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]

If you have any questions, please tell me.

CodePudding user response:

You could have a look at convtools library, which provides lots of data processing primitives

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"],
    ],
}
  • Related