0

I'm trying to reshape a pandas dataframe to display properties of a single object (column1) that are organized line by line into a single line per object with multiple columns and NaNs in case the properties are missing, like the second table.

Input table

Expected output

I've found the exactly opposite (pd.melt) but so far couldn't reverse the process.

Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
moises
  • 1
  • 1
    can you paste your dataframe instead of putting a screenshot? – ignoring_gravity Jan 08 '20 at 14:00
  • use this and then change the column names: `final=df.set_index(['Column1',df.groupby('Column1').cumcount()]).unstack().sort_index(level=1,axis=1).reset_index()` and then `final.columns=[f"Column{i}" for i in range(1,final.shape[1]+1)]` – anky Jan 08 '20 at 14:05
  • Thanks for the answers. I solved it with: df.pivot(index='Column1', columns='Column3', values='Column2') – moises Jan 14 '20 at 21:09

0 Answers0