Home > Enterprise >  Group / Pivot Dictionaries Python
Group / Pivot Dictionaries Python

Time:11-05

I have a dictionary I would like to pivot or group without pandas:

Here is what I have

Dict = [{'eventTimeMinutes': '2021-10-31 00:00', 'View': 4},
{'eventTimeMinutes': '2021-10-31 00:00', 'Delete': 4},
{'eventTimeMinutes': '2021-10-31 00:00', 'Create': 2},
{'eventTimeMinutes': '2021-10-31 00:00', 'Modify': 1},
{'eventTimeMinutes': '2021-10-31 00:01', 'View': 4},
{'eventTimeMinutes': '2021-10-31 00:01', 'Delete': 1},
{'eventTimeMinutes': '2021-10-31 00:01', 'Create': 1},
{'eventTimeMinutes': '2021-10-31 00:01', 'Modify': 1}]

I would like to have it like this

[{'eventTimeMinutes': '2021-10-31 00:00',
  'View': 4,
  'Delete': 4,
  'Create': 2,
  'Modify': 1
 },
 {'eventTimeMinutes': '2021-10-31 00:01',
  'View': 4,
  'Delete': 1,
  'Create': 1,
  'Modify': 1
 }
]

CodePudding user response:

Just iterate through, adding a new entry when the minutes change. This assumes the list is sorted by time. Note that you call this a Dict, but it's not a dict, it's a list.

Dict = [{'eventTimeMinutes': '2021-10-31 00:00', 'View': 4},
{'eventTimeMinutes': '2021-10-31 00:00', 'Delete': 4},
{'eventTimeMinutes': '2021-10-31 00:00', 'Create': 2},
{'eventTimeMinutes': '2021-10-31 00:00', 'Modify': 1},
{'eventTimeMinutes': '2021-10-31 00:01', 'View': 4},
{'eventTimeMinutes': '2021-10-31 00:01', 'Delete': 1},
{'eventTimeMinutes': '2021-10-31 00:01', 'Create': 1},
{'eventTimeMinutes': '2021-10-31 00:01', 'Modify': 1}]

d2 = []
for row in Dict:
    if not d2 or d2[-1]['eventTimeMinutes'] != row['eventTimeMinutes']:
        d2.append( row )
    else:
        d2[-1].update(row)
print(d2)     

CodePudding user response:

You can use groupby from itertools to group the dictionaries on eventTimeMinutes, but you need to sort the list first in-order for grouping to work:

from itertools import groupby
result = []
for _, value in groupby(sorted(data, key=lambda x:x['eventTimeMinutes']),
                        key= lambda x: x['eventTimeMinutes']):
    temp = {}
    for d in value:
        temp.update(**d)
    result.append(temp)

OUTPUT:

>>> result

[{'eventTimeMinutes': '2021-10-31 00:00',
  'View': 4,
  'Delete': 4,
  'Create': 2,
  'Modify': 1},
 {'eventTimeMinutes': '2021-10-31 00:01',
  'View': 4,
  'Delete': 1,
  'Create': 1,
  'Modify': 1}]
  • Related