0

I have a dataframe that looks like the following: https://i.stack.imgur.com/EfFrp.png

I want to transform this data frame into a nested dictionary where I have for a single key ID, multiple key values corresponding to each Vest Date 1, 2, 3 and 4, and the corresponding value is the Vest Quantity, for example:

dict={'211':{'2019-08-01': 75},
            {'2020-08-01': 75},
            {'2021-08-01': 75},
            {'2022-08-01': 75},
            {'2021-12-01': 71},
            {'2022-12-01': 72},
            {'2023-12-01': 72},
            {'2024-12-01': 72},
            ...
     '7674':{'2019-07-01': 64},
            {'2020-07-01': 65},
            {'2021-07-01': 64},
            {'2022-07-01': 64}
      }

Then, I would like to sort by dates for each ID.

For the dictionary, I tried first to use melt but it combines all dates and quantities into equal columns, so any help is appreciated!

Thanks in advance!

  • You have only given an **image** of the input, so we cannot reproduce. Please show the input dataframe as copyable **text** in the question. [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) is a good starting point to understand how to do that... – Serge Ballesta Jul 19 '21 at 09:07
  • @SergeBallesta, is there a shortcut to pandas example link? Like with `[mre]` for [mre]? – Andreas Jul 19 '21 at 09:55
  • @Andreas: good question, but I do not know the answer... The reason why I had to manually copy the title and the URL :-( – Serge Ballesta Jul 19 '21 at 10:01

0 Answers0