0

I currently have a dataset that is structured like so:

ID     ProgramCode     ProgramType     
001    0001            Major
001    0423            Minor
002    3203            Major
002    5893            Minor

I am trying to figure out the best way to use Pandas to Pivot the data to produce an output like:

ID     ProgramCode_1         ProgramType_1      ProgramCode_2       ProgramType_2
001    0001                  Major              0423                Minor
002    3203                  Major              5893                Minor

I have tried various parameters using

df.pivot()

with no success.

Is there a better Pandas function to essentially search the DF for unique IDs and then fill in columns across a single row?

Hayden
  • 498
  • 2
  • 5
  • 18
  • Check question 10 and 11 – BENY Apr 01 '20 at 17:57
  • 2
    use: `new_df = df.pivot_table(index = 'ID', columns = df.groupby('ID').cumcount().add(1), values = ['ProgramCode', 'ProgramType'], aggfunc='first').sort_index(axis=1,level=1)` and then `new_df.columns = [f'{x}_{y}' for x,y in new_df.columns]` – ansev Apr 01 '20 at 18:00

0 Answers0