0

I have the following data in a Pandas data frame:

Cavity Dimension
1 1.45
1 1.47
1 1.46
1 1.45
1 1.43
2 1.40
2 1.39
2 1.41
2 1.40
2 1.42
3 1.51
3 1.5
3 1.49
3 1.5
3 1.52

how can I reshape the data to the following format in pandas?

Cavity 1 Cavity 2 Cavity 3
1.45 1.40 1.51
1.47 1.39 1.50
1.46 1.41 1.49
1.45 1.40 1.50
1.43 1.42 1.52

is there any way to do this reshape (melt backwards)?

thanks for your help!!

Luis
  • 11
  • 3
  • Use `df = df.assign(g = df.groupby('Cavity').cumcount()).pivot(index='g', columns='Cavity', values='Dimension').add_prefix('Cavity ')` – jezrael May 18 '23 at 05:50

0 Answers0