Home > Net >  Column List of Dictionaries Create Columns
Column List of Dictionaries Create Columns

Time:12-11

I have a column, X, where the rows' values are a list of dictionaries. I.e.

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

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

How can I create columns of ea. key present in X to then populate with values from the list of dictionaries from ea. row?

So for row 1, I'd populate a column, 1, with 1444... and column 3, with 12, 11 (2 values separated by a comma).

CodePudding user response:

Here's one way to solve it.

Given:

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

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

Format the rows so that they look like:

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

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

Then, you can just loop over each row and call .join().

Here is some pseudocode :)

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(',')  

CodePudding user response:

You can first create a dictionary data where columns are indices and indices and columns. Then from that reverse the order and create dictionary 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}

Outcome:

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

Instead of the second part, you may also do:

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