0

I would like to convert a large dataframe (100.000, 28) from long to wide format.

Currently I have:

ID_num Col_A Col_B Col_C
001 x y z
001 x l m
002 w k p
002 w t q

I want to have:

ID_num Col_A Col_B_1 Col_B_2 Col_C_1 Col_C_2
001 x y l z m
002 w k t p q

So I have a few columns that have the same values for multiple entries of ID_num, and I would like new columns with an index number behind the column name, for each repeated entry. This is also how SPSS does it.

The number of repeated entries differs between IDs, some have up to 30 entries, so the naming of columns should go up to that number.

I've tried the solutions here: Pandas long to wide reshape, by two variables but ran into trouble when trying to do this for all of the columns, not just two.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
NobL
  • 1

0 Answers0