3

I've data stored in pandas dataframe and I want to create a nested dictionary using that data. The data looks like as follows:

+---------+------+----------+----------+---------------+-------+
| Product | Zone |  start   |   end    |      seq      | store |
+---------+------+----------+----------+---------------+-------+
| A       | E/A  | 08:00:00 | 17:40:00 | 0, 1, 2, 3, 4 | Z,X   |
| B       | A/N  | 08:30:00 | 16:00:00 | 0, 1, 2, 3, 4 | BB    |
| AS      | A/S  | 11:00:00 | 16:00:00 | 0, 1, 2, 3, 4 | CD,DD |
+---------+------+----------+----------+---------------+-------+

Using the data above stored in pandas dataframe I want to create a nested python dict. If created dict is named stores than output from following code is shown below:

for store in list(stores.items()):
    print(store)

Desired Output:

('A', {'Zone': 'E/A', 'tp': [{'start': [8, 0], 'end': [17, 40], 'seq': [0, 1, 2, 3, 4]}], 'store': ['Z','X']})
('B', {'Zone': 'A/N', 'tp': [{'start': [8, 30], 'end': [16, 0], 'seq': [0, 1, 2, 3, 4]}], 'store': ['BB']})
('AS', {'Zone': 'A/S', 'tp': [{'start': [11, 0], 'end': [16, 0], 'seq': [0, 1, 2, 3, 4]}], 'store': ['CD', 'DD']})

I am looking for a solution that takes pandas data frame as input and gives results as shown above.

Lopez
  • 461
  • 5
  • 19

0 Answers0