I'm finding an efficient to reshape a N*M dataframe to 1*(N*M) dataframe:
INPUT>
df1
ID distUnit col_a col_b
1000 150 35 55
1000 250 10 20
1200 150 12 13
1200 250 16 20
DESIRED OUTPUT>
ID col_a_150 col_b_150 col_a_250 col_b_250
1000 35 55 10 20
1200 12 13 16 20
My idea>
- Go through every row in df1
- add prefix to col_a and col_b based on the value of
row['distUnit']
- using
combined_first
to add processed row back to result dataframe
Challenging part >
Since the size of my input data is 14440 * 20, my idea is not efficient enough.
Wondering any better implementation ways to solve this?
Thanks for reading.