1

My dataset consists out of two columns, PersonID and Request for application(Aanvraag datum). Some PersonIDs occur multiple times because these persons received multiple request on different dates:

enter image description here

Now I want to create multiple request columns per PersonID. For example if a PersonID occurs three times, then the code creates three columns for three different requests dates. If a PersonID occurs one time, the code assigns NaN to the empty requests. In the end there will be as many columns as the PersonID with the most requests.

Example: enter image description here

Many thanks!

roet
  • 27
  • 6
  • 1
    Does this answer your question? [How can I pivot a dataframe?](https://stackoverflow.com/questions/47152691/how-can-i-pivot-a-dataframe), [pivot-a-dataframe-in-pandas](https://stackoverflow.com/questions/70411391/pivot-a-dataframe-in-pandas/70411430#70411430) – Patryk Kowalski Dec 20 '21 at 09:14

1 Answers1

0

Use GroupBy.cumcount with DataFrame.set_index and append=True and then Series.unstack with DataFrame.add_prefix:

df1 = (df.set_index(df.groupby(level=0).cumcount().add(1), append=True)['Aanvraag datum']
        .unstack()
        .add_prefix('Aanvraag '))
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252