gpt4 book ai didi

python - Pandas DataFrame 到列表列表的字典

转载 作者:行者123 更新时间:2023-12-01 00:06:36 25 4
gpt4 key购买 nike

我有一个包含四列的 CSV 文件:product_price、country_of_origin、product_quantity 和brand_id。 This is what csv looks like我想创建一个字典,其中键是brand_id,值是包含其他列的元组/列表的列表。类似这样的事情:

some_dict = {
1:
[(country_of_origin, product_quantity, product_price),
(country_of_origin, product_quantity, product_price),
(country_of_origin, product_quantity, product_price)],
2:
[(country_of_origin, product_quantity, product_price),
(country_of_origin, product_quantity, product_price)],
3:
[(country_of_origin, product_quantity, product_price),
(country_of_origin, product_quantity, product_price)]
}

是否可以用 pandas 创建这样的结构?我尝试过在 df.itertuples(index=False)} 中使用 {x[3]: x[0:] for x in df.itertuples(index=False)} 但它只返回每个 Brand_id 一个值:

{1: (200, 'Kenya', 19), 3: (40, 'South Africa', 40), 2: (350, 'Turkey', 64)}

最佳答案

您可以将字典理解groupby结合使用brand_idDataFrame.iterrows :

some_dict = {k: [(co, pq, pp) for _, (pp, co, pq, _) in x.iterrows()]
for k, x in df.groupby('brand_id')}

[输出]

{1: [('Kenya', 19, 200), ('Turkey', 25, 35), ('Jordan', 53, 16)],
2: [('Turkey', 64, 350), ('Jordan', 24, 80)],
3: [('South Africa', 5, 40), ('Oman', 8, 63)]}

关于python - Pandas DataFrame 到列表列表的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59933721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com