-1

Image containing the problem, click here

Please review the image

  • 1
    ```df.melt(id_vars='Campus',var_name='Month',value_name='Value')``` Please have a read [here](https://stackoverflow.com/help/how-to-ask) before you post your next question :) – sophocles Jan 08 '22 at 09:11
  • 1
    Instead of the image, please copy and paste the text. In this way it will be easier for the others to reuse the text for testing. – Marcel Preda Jan 08 '22 at 09:12
  • [This reply](https://stackoverflow.com/a/70631466/17850902) solves your problem and explains the solution. – jgru Jan 08 '22 at 10:33

1 Answers1

0

You can achieve this by using the function melt(), which to massage a DataFrame into a format where one or more columns are identifier variables (here Campus), while all other columns, considered measured variables (here Apr, May, June`), are "unpivoted" to the row axis as the following code snippet illustrates:

import pandas as pd

df1 = pd.DataFrame({'Campus':  ['A', 'B'],
        'Apr': [55, 1],
        'May': [56, 2],
        'June': [57, 3],
        })

df2 = df1.melt(id_vars='Campus',var_name='Month',value_name='Value')

df2 has then the following content:


    Campus  Month   Value
0   A       Apr     55
1   B       Apr     1
2   A       May     56
3   B       May     2
4   A       June    57
5   B       June    3

jgru
  • 181
  • 5