Search code examples
pythonpandasdataframepandas-melt

Pandas melt and ordered based on another columns


I have a data frame like

import pandas as pd
data = {'Type': ['Fruits', 'Fruits', 'Fruits', 'Fruits'],
        'Name': ['Mango', 'Mango', 'Mango', 'Mango'],
        'Variety': ['Alphonso', 'Dasheri', 'Langra', 'Raspuri'],
        'April':[120,110,90,60],
        'May':[110,80,50,40],
        'June':[80,110,76,65],
        'July':[85,87,55,50]}
df = pd.DataFrame(data)
df=df[['Type','Name','Variety','April','May','June','July']]
     Type   Name   Variety  April  May  June  July
0  Fruits  Mango  Alphonso    120  110    80    85
1  Fruits  Mango   Dasheri    110   80   110    87
2  Fruits  Mango    Langra     90   50    76    55
3  Fruits  Mango   Raspuri     60   40    65    50

When I am doing pandas melt over above dataframe, I am getting like

ndf=df.melt(id_vars=['Type','Name','Variety'],var_name="Month",value_name="Price")
     Type   Name   Variety  Month  Price
0   Fruits  Mango  Alphonso  April    120
1   Fruits  Mango   Dasheri  April    110
2   Fruits  Mango    Langra  April     90
3   Fruits  Mango   Raspuri  April     60
...........
11  Fruits  Mango   Raspuri   June     65
12  Fruits  Mango  Alphonso   July     85
13  Fruits  Mango   Dasheri   July     87
14  Fruits  Mango    Langra   July     55
15  Fruits  Mango   Raspuri   July     50

But actually I need the data frame ordered based on "variety" instead of "month". The expected dataframe is like

     Type   Name   Variety  Month  Price
0   Fruits  Mango  Alphonso  April    120
1   Fruits  Mango  Alphonso    May    110
2   Fruits  Mango  Alphonso   June     80
3   Fruits  Mango  Alphonso   July     85
4   Fruits  Mango   Dasheri  April    110
5   Fruits  Mango   Dasheri    May     80
.................................
13  Fruits  Mango   Raspuri    May     40
14  Fruits  Mango   Raspuri   June     65
15  Fruits  Mango   Raspuri   July     50

What is the solution for this?


Solution

  • You just need to sort the values in the dataframe by that column such as

    ndf = ndf.sort_values(by=['Variety'], ascending = False)