字典的列列表创建列

Column List of Dictionaries Create Columns

我有一列 X,其中行的值是字典列表。即

row 1 = [{category: **1**, outcome: 1444}, {category: **3**, outcome: 12}, {category: **3**, outcome: 11},... ]

row 2 = [{category: 2, outcome: 1555}, {category: 5, outcome: 42},...]

我如何为 X 中存在的每个键创建列,然后用每行的字典列表中的值填充?

因此,对于第 1 行,我将在 1 列中填充 1444... 并在 3 列中填充 12 , 11(2 个值用逗号分隔)。

这是解决问题的一种方法。

鉴于:

row 1 = [{category: 1, outcome: 1444}, {category: 3, outcome: 12}, {category: 3, outcome: 11},... ]

row 2 = [{category: 2, outcome: 1555}, {category: 5, outcome: 42},...]

格式化行,使它们看起来像:

new_row_1 = { 
     1: [1444, ...],
     3: [1444, 12, ...], 
      ...
}

new_row_2 = {
     2: [1555, ...], 
     5: [42, ...],
     ...
}

然后,您可以遍历每一行并调用 .join()

这是一些伪代码:)

RESULT_GROUPS = {}
FOREACH row_id, row in rows
   FOREACH item in row
      RESULT_GROUPS[row_id][ item[category] ] = item[value]

   FOREACH group_id, group in RESULT_GROUPS[row_id]
      RESULT_GROUPS[row_id][ group_id ] = group.join(',')  

您可以先创建一个字典 data,其中列是索引,索引和列。然后从中反转顺序并创建字典 out:

rows = [row_1, row_2]

data = {}
for i, row in enumerate(rows):
    new_data = {}
    for dct in row:
        new_data.setdefault(dct['category'], []).append(dct['outcome'])
    data[i] = {k:(v[0] if len(v)==1 else v) for k,v in new_data.items()}
        
out = {}
for key, dct in data.items():
    for k,v in dct.items():
        out[k] = {key:v}

结果:

{1: {0: 1444}, 3: {0: [12, 11]}, 2: {1: 1555}, 5: {1: 42}}

除了第二部分,您还可以这样做:

out = pd.DataFrame(data).to_dict()