0
                 Jan  Feb  Mar   
Supplier01       100  100  100  
Supplier02       200  200  200 
Supplier03      5000   49  359  
Supplier04       500  500  500   

I have managed to get the above dataframe using the below code

monthset=["Jan","Feb","Mar"]
supplierset=["Supplier01","Supplier02","Supplier03","Supplier04"]

data is a list of list as below

data=[[100,100,100],[200,200,200],[5000,49,359],[500,500,500]]
df = pd.DataFrame(data,columns=monthset,index=supplierset)

How can I get an output as bellow

Supplier01,Jan,100
Supplier01,Feb,100

etc.. Basically infront of an elemnt value the column name and index name with comma seperated.

suresh_chinthy
  • 377
  • 2
  • 12

1 Answers1

1
import pandas as pd

df['supplier'] = df.index
pd.melt(df, id_vars=['supplier'], var_name=['Month'], value_name='Value')

Output:

      supplier Month  Value
0   Supplier01   Jan    100
1   Supplier02   Jan    200
2   Supplier03   Jan   5000
3   Supplier04   Jan    500
4   Supplier01   Feb    100
5   Supplier02   Feb    200
6   Supplier03   Feb     49
7   Supplier04   Feb    500
8   Supplier01   Mar    100
9   Supplier02   Mar    200
10  Supplier03   Mar    359
11  Supplier04   Mar    500
user2077935
  • 358
  • 3
  • 12
  • Iam getting an error as 'supplier' is not defined.Looks like and 'supplier is an object and based on that melting is not happening.. – suresh_chinthy Apr 28 '20 at 06:35