English 中文(简体)
Pandas 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

当我超负荷工作时,我正在像现在这样做。

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

但实际上,我需要根据“惯例”而不是“月”订购的数据框架。 预期的数据范围类似

     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

有什么解决办法?

最佳回答

仅需要将数据框中的数值按该栏,例如:

ndf = ndf.sort_values(by=[ Variety ], ascending = False)
问题回答

在您使用pd.melt之后,你可以根据Valiety栏对数据进行分类。

ndf.sort_values(by=[ Variety ]).reset_index(drop=True, inplace=True)
ndf

Here s the documentation for the sort_values method https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.sort_values.html

结果:

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
6 Fruits Mango Dasheri June 110
7 Fruits Mango Dasheri July 87
8 Fruits Mango Langra April 90
9 Fruits Mango Langra May 50
10 Fruits Mango Langra June 76
11 Fruits Mango Langra July 55
12 Fruits Mango Raspuri April 60
13 Fruits Mango Raspuri May 40
14 Fruits Mango Raspuri June 65
15 Fruits Mango Raspuri July 50

Edit 1: As per mozway s suggestion you don t need to reset the index after, but can instead pass ignore_index=True as a parameter to sort_values()

ndf.sort_values(by=[ Variety ], ignore_index=True, inplace=True)
ndf

在<代码>melt后不打上列栏,你还可以改变重塑方法。

If you stack this will directly preserve the original order of rows/columns:

out = (df.rename_axis(columns= Month )
         .set_index([ Type , Name , Variety ])
         .stack().reset_index(name= Price )
      )

产出:

      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
6   Fruits  Mango   Dasheri   June    110
7   Fruits  Mango   Dasheri   July     87
8   Fruits  Mango    Langra  April     90
9   Fruits  Mango    Langra    May     50
10  Fruits  Mango    Langra   June     76
11  Fruits  Mango    Langra   July     55
12  Fruits  Mango   Raspuri  April     60
13  Fruits  Mango   Raspuri    May     40
14  Fruits  Mango   Raspuri   June     65
15  Fruits  Mango   Raspuri   July     50




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...