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
有什么解决办法?