python - Convert columns into rows with Pandas - Stack Overflow

PHOTO EMBED

Thu Jun 02 2022 11:40:23 GMT+0000 (Coordinated Universal Time)

Saved by @vigneshprince #python

df1 = (df.set_index(["location", "name"])
         .stack()
         .reset_index(name='Value')
         .rename(columns={'level_2':'Date'}))
print (df1)
  location  name        Date  Value
0        A  test    Jan-2010     12
1        A  test    Feb-2010     20
2        A  test  March-2010     30
3        B   foo    Jan-2010     18
4        B   foo    Feb-2010     20
5        B   foo  March-2010     25
content_copyCOPY

https://stackoverflow.com/questions/28654047/convert-columns-into-rows-with-pandas