Python/Excel/SQL列日期到分解行
我有一个Excel电子表格,如下图所示,其中显示了2019年和2020年每周售出的单位数量(列),我正试图将其放入access数据库中,格式如第二个表格:Python/Excel/SQL列日期到分解行,python,sql,excel,pandas,Python,Sql,Excel,Pandas,我有一个Excel电子表格,如下图所示,其中显示了2019年和2020年每周售出的单位数量(列),我正试图将其放入access数据库中,格式如第二个表格: | model | 2019 - Wk 1 U's | 2019 - Wk 2 U's | 2019 - Wk 3 U's | ... | 2020 - Wk 52 U's | -----------------------------------------------------------------------------------
| model | 2019 - Wk 1 U's | 2019 - Wk 2 U's | 2019 - Wk 3 U's | ... | 2020 - Wk 52 U's |
---------------------------------------------------------------------------------------
abc 50 45 65 20
def 72 93 57 68
我想这样做
| model | year | month | week | units |
---------------------------------------
abc 2019 1 1 50
abc 2019 1 2 45
abc 2019 1 3 65
def 2019 1 5 72
def 2019 1 6 93
def 2019 1 7 57
... .... .. .. ..
abc 2020 12 52 20
def 2020 12 52 68
在Python(pandas)或VBA/Excel公式(或SQL,如果需要的话)中,最好的方法是什么?只需要系统化
设置索引(“模型”)
df.columns.set_名称(“weekencoded”,inplace=True)
stack()
reset_index()
这样编码的字符串就可以作为列使用model weekencoded units year week
abc 2019 - Wk 1 U's 50 2019 1
abc 2019 - Wk 2 U's 45 2019 2
abc 2019 - Wk 3 U's 65 2019 3
abc 2020 - Wk 52 U's 20 2020 52
def 2019 - Wk 1 U's 72 2019 1
def 2019 - Wk 2 U's 93 2019 2
def 2019 - Wk 3 U's 57 2019 3
def 2020 - Wk 52 U's 68 2020 52
只是需要系统化
设置索引(“模型”)
df.columns.set_名称(“weekencoded”,inplace=True)
stack()
reset_index()
这样编码的字符串就可以作为列使用model weekencoded units year week
abc 2019 - Wk 1 U's 50 2019 1
abc 2019 - Wk 2 U's 45 2019 2
abc 2019 - Wk 3 U's 65 2019 3
abc 2020 - Wk 52 U's 20 2020 52
def 2019 - Wk 1 U's 72 2019 1
def 2019 - Wk 2 U's 93 2019 2
def 2019 - Wk 3 U's 57 2019 3
def 2020 - Wk 52 U's 68 2020 52
这张桌子有多大?第一个表包含月份吗?@xlsx第一个表没有显示月份,但很遗憾,我可以在之后手动添加。桌子有800行。桌子有多大?第一个表包含月份吗?@xlsx第一个表没有显示月份,但很遗憾,我可以在之后手动添加。表中有800行。我自己永远也无法做到这一点。有没有办法我也能扭转它,让它回到以前的样子?太棒了。我自己永远也无法做到这一点。有没有一种方法可以让我也扭转它,让它回到以前的样子?