[英]Pandas DataFrame to dictionary of lists of lists
I have CSV file with four columns: product_price, country_of_origin, product_quantity and brand_id.我有包含四列的 CSV 文件:product_price、country_of_origin、product_quantity 和 brand_id。 This is what csv looks like I want to create a dictionary where the key will be brand_id and the value will be a list of tuple/lists with other columns.这就是 csv 的样子我想创建一个字典,其中键是brand_id,值是包含其他列的元组/列表列表。 Something like that:类似的东西:
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)]
}
Is it possible to create such structure with pandas?有可能用熊猫创建这样的结构吗? I've tried using {x[3]: x[0:] for x in df.itertuples(index=False)} but it returns only one value per brand_id:我试过在 df.itertuples(index=False)} 中使用 {x[3]: x[0:] for x 但它每个brand_id 只返回一个值:
{1: (200, 'Kenya', 19), 3: (40, 'South Africa', 40), 2: (350, 'Turkey', 64)}
You can use dict comprehension
with groupby
brand_id
and DataFrame.iterrows
:您可以将dict comprehension
与groupby
brand_id
和DataFrame.iterrows
:
some_dict = {k: [(co, pq, pp) for _, (pp, co, pq, _) in x.iterrows()]
for k, x in df.groupby('brand_id')}
[out] [出去]
{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)]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.