Home > Mobile >  Pandas Transpose part of dataframe
Pandas Transpose part of dataframe

Time:07-13

I try to transpose a part of dataframe but I don't if I can have the result needed.

My df is like (excel extract):

code A code B June July August
324325 3243. 0,3. 0,5. 0,4.
884543 7954. 0,1. 0,5. 0,43.

And I try to have:

code A code B Month Value
324325 3243. June 0,3.
324325 3243. July 0,5.
324325 3243. August 0,4.
884543 7954. June 0,1.
884543 7954. July 0,5.
884543 7954. August 0,43.

Is it possible to have it by easy way in pandas of I have to do some code ?

CodePudding user response:

Your transpose is a dataframe melt here:

pd.melt(df, id_vars=['code A', 'code B'], value_vars=['June', 'July', 'August'], var_name='Month')
  • Related