0

I was trying to turn a number of columns to long format (wide to long). I tried this solution and I didn't seem to get it right. How can I get this right by pivoting the "London, New York, Boston" columns to long format?

df = pd.DataFrame({
    'date' : ['2021-08-01', '2021-08-01', '2021-08-01', '2021-08-02', '2021-08-02', '2021-08-02'],
    'person': ['type_A', 'type_C', 'type_C', 'type_A', 'type_B', 'type_D'],
    'London' : [1, 4, 7, 5, 7, 9],
    'New York' : [2, 5, 8, 7, 9, 10],
    'Boston' : [3, 6, 9, 1, 2, 7]
})
df['id'] = df.index
pd.wide_to_long(df, ["place"], i="id", j="value")
codedancer
  • 1,504
  • 9
  • 20

0 Answers0