如何用Python来处理数据表的长宽转换(图文详
答案:1 悬赏:70 手机版
解决时间 2021-03-16 13:39
- 提问者网友:低吟詩仙的傷
- 2021-03-15 20:38
如何用Python来处理数据表的长宽转换(图文详
最佳答案
- 五星知识达人网友:洎扰庸人
- 2021-03-15 21:25
import pandas as pd
import numpy as np
mydata=pd.DataFrame({"Name":["苹果","谷歌","脸书","亚马逊","腾讯"],"Conpany":["Apple","Google","Facebook","Amozon","Tencent"],"Sale2013":[5000,3500,2300,2100,3100],"Sale2014":[5050,3800,2900,2500,3300],"Sale2015":[5050,3800,2900,2500,3300],"Sale2016":[5050,3800,2900,2500,3300]
})
mydata1=mydata.melt(
id_vars=["Name","Conpany"], #要保留的主字段
var_name="Year", #拉长的分类变量
value_name="Sale" #拉长的度量值名称
)
import numpy as np
mydata=pd.DataFrame({"Name":["苹果","谷歌","脸书","亚马逊","腾讯"],"Conpany":["Apple","Google","Facebook","Amozon","Tencent"],"Sale2013":[5000,3500,2300,2100,3100],"Sale2014":[5050,3800,2900,2500,3300],"Sale2015":[5050,3800,2900,2500,3300],"Sale2016":[5050,3800,2900,2500,3300]
})
mydata1=mydata.melt(
id_vars=["Name","Conpany"], #要保留的主字段
var_name="Year", #拉长的分类变量
value_name="Sale" #拉长的度量值名称
)
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯