将csv转换为具有多个值的dict

pb3skfrl  于 2023-03-27  发布在  其他
关注(0)|答案(4)|浏览(144)

我有一个CSV,当加载时看起来像这样。

chicken, meat
veal, meat
rice, carbs
potato, carbs
carrot, veggies
mushroom, veggies
apples, fruits

我想从它创建一个字典,所以我使用以下代码:

food = pd.read_csv('foods.csv',  header=None, index_col=1, squeeze=False).to_dict()

当我打印字典时,每个键只显示一个条目。相反,我希望所有条目都像这样显示。

{'carbs':['potato','rice'],
'meat':['chicken','veal'],
'veggies':['mushroom','carrot'],
'fruits':['apples']}
7nbnzgx9

7nbnzgx91#

你可以跳过Pandas直接处理文件。因为你实际上有一个两个字符的分隔符', ',所以跳过csv也更容易:

di={}
with open('/tmp/fruit.csv') as f:
    for x,y in (line.rstrip().split(', ') for line in f):
        di.setdefault(y, []).append(x)

>>> di
{'meat': ['chicken', 'veal'], 'carbs': ['rice', 'potato'], 'veggies': ['carrot', 'mushroom'], 'fruits': ['apples']}

或者使用pandas:

df=pd.read_csv('/tmp/fruit.csv',  header=None, sep=', ', engine='python').groupby([1])[0].agg(list).to_dict()

>>> df
{'carbs': ['rice', 'potato'], 'fruits': ['apples'], 'meat': ['chicken', 'veal'], 'veggies': ['carrot', 'mushroom']}
deyfvvtc

deyfvvtc2#

如果你不需要Pandas,这可以很容易地使用Python的CSV阅读器来完成。

import csv
from collections import defaultdict

category_food_map = defaultdict(list)

with open('foods.csv', newline='') as f:
    reader = csv.reader(f)

    for row in reader:
        food, category = row
        category = category.strip()

        category_food_map[category].append(food)

for category, food in category_food_map.items():
    print(f'{category}: {food}')

我得到:

meat: ['chicken', 'veal']
carbs: ['rice', 'potato']
veggies: ['carrot', 'mushroom']
fruits: ['apples']
tyg4sfes

tyg4sfes3#

有一个纯python convtools库,它提供了大量的数据处理原语并促进了函数式方法:

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

# store converter for future reuse
converter = (
    c.group_by(c.item(1))
    .aggregate(
        {
            # use c.ReduceFuncs.ArrayDistinct(c.item(0)) if you need only
            # unique values in lists
            c.item(1): c.ReduceFuncs.Array(c.item(0)),
        }
    )
    .gen_converter()
)

# stream reading and processing if needed
rows = Table.from_csv(
    "tmp2.csv", dialect=Table.csv_dialect(skipinitialspace=True)
).into_iter_rows(list)
result = converter(rows)

assert result == [
    {'meat': ['chicken', 'veal']},
    {'carbs': ['rice', 'potato']},
    {'veggies': ['carrot', 'mushroom']},
    {'fruits': ['apples']}]
yvfmudvl

yvfmudvl4#

df1.groupby(" meat").agg(list).chicken.to_dict()

输出:

{' carbs': ['rice', 'potato'],
 ' fruits': ['apples'],
 ' meat': ['veal'],
 ' veggies': ['carrot', 'mushroom']}

相关问题