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

标签 python pandas

我有一个包含四列的 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/

相关文章:

python - Pandas .loc 多重赋值与单个赋值

python - webdriver.get() 引发 TimeoutException

python - 使用 Pandas,如何根据 "master sheet"中的一列组合多个工作表?

python - 将日期/小时数据帧解压到带有日期时间索引的单列中 - python、pandas

python - print(tabulate(...)) 漂亮地打印 multiIndex pandas ?

python - 属性错误 : 'NoneType' object has no attribute 'ids' (self. 根返回 'None' )

python - 将 pandas 条形图的图例与次要 y 轴放在条形图前面

python - Python 中的字符串 + 变量 + 字符串格式化

python - UTC 到给定国家/地区首字母缩写的本地时间

python - 加速或矢量化 pandas 应用函数 - 需要有条件地应用函数