Skip to content
Advertisement

Pandas how to pivot/unpivot/add a dummy column name

I want to convert from a long to a wide table with dummy column names created based on the number of accid

sample excel input vs output attached

enter image description here

Please help

Advertisement

Answer

I was able to get down to 2 steps, pivot_table using aggfunc=list, and then creating new columns from that list.

I’m not sure I’ve come up with what you want though, because the assignment to columns is just filling up from the left.

Create the DataFrame:

JavaScript

Pivot table:

JavaScript

New columns from list:

JavaScript

Changing the column names:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement