I need to reshape a DataFrame from long to wide format. Example of what the data looks like:
import pandas as pd
df = pd.DataFrame({'id':[1,1,1,2,2,2,3,3], 'id_age':
30,30,30,23,23,23,29,29], 'product':['A','B','C','A','B','C','A','B'],
'rank':[1,2,3,3,1,2,2,1], 'result':['x','y','z','p','q','r','s','t']})
print(df)
id id_age product rank result
0 1 30 A 1 x
1 1 30 B 2 y
2 1 30 C 3 z
3 2 23 A 3 p
4 2 23 B 1 q
5 2 23 C 2 r
6 3 29 A 2 s
7 3 29 B 1 t
The desired output is
id id_age product rank result
0 1 30 [A, B, C] [1, 2, 3] [x, y, z]
1 2 23 [A, B, C] [3, 1, 2] [p, q, r]
2 3 29 [A, B] [2, 1] [s, t]
i.e. one row per id. I have tried to solve the problem by creating a pivot table, but I cannot figure out how to:
- reshape it for more than one column (product, rank, result)
- handle the values that are constant within individuals (id_age)
- get the columns in the final data frame to be in list format (and the order is important, the first value of the list in rank corresponds to the first value of the list in result for a given individual).
Any recommendations on how to move forward on this issue is appreciated!