0

Similar question:

(I got duplicate index error when using the method given in that link)

MWE

df_long = pd.DataFrame({'name': ['A', 'B', 'A', 'B'],
          'variable': ['height', 'height', 'width', 'width'],
          'value': [10, 20, 1, 2]})
print(df_long)
  name variable  value
0    A   height     10
1    B   height     20
2    A    width      1
3    B    width      2



============================

Requires answer

  name  height  width
0    A      10      1
1    B      20      2

My attempt

(df_long.set_index(['name'])
        .stack()
        .unstack(0)
        .reset_index()
        .rename_axis(None, axis=1)
)

ValueError: Index contains duplicate entries, cannot reshape
BhishanPoudel
  • 15,974
  • 21
  • 108
  • 169

1 Answers1

1
df_long.pivot_table("value",["name"], "variable")

variable  height  width
name                   
A             10      1
B             20      2
Avi Thaker
  • 455
  • 3
  • 10