[英]How to consolidate multiple CSV files with similar and different columns into 1 using Python and Pandas?
[英]Consolidate multiple table in single sheet with similar headers using power query or python
在 excel 表中,我有多个具有类似 header 的表。 我需要帮助将所有表合并到一个表中。 我的表结构如下所示。
表格1:
项目1 | 2018 | 2019 | 2020 |
---|---|---|---|
计划 | 100 | 200 | 300 |
预报 | 20 | 40 | 30 |
表 2:
项目2 | 2018 | 2019 | 2020 |
---|---|---|---|
计划 | 400 | 500 | 600 |
预报 | 40 | 50 | 60 |
output表:
项目 | 行动 | 2018 | 2019 | 2020 |
---|---|---|---|---|
项目1 | 计划 | 100 | 200 | 300 |
项目1 | 预报 | 10 | 20 | 30 |
项目2 | 计划 | 400 | 500 | 600 |
项目2 | 预报 | 40 | 50 | 60 |
我需要使用 python 或电源查询自动执行此操作,以便我可以在 PowerBI 中使用它进行可视化
请帮助我正确的方法和必要的步骤。
表实结构:输入-https://imgur.com/OuUpimS
输出-https://imgur.com/esb30xv
使用电源查询
突出显示每个表并使用
数据..来自表/范围... [x] 我的表有标题
要将列 header 添加为重复行,添加列.. 自定义列.. 名称为项目和公式
= Table.DemoteHeaders(#"Changed Type"){0}[Column1]
使用文件保存并关闭...关闭并加载到..仅创建连接..
每个表的完整代码将与此类似
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"project1", type text}, {"2018", Int64.Type}, {"2019", Int64.Type}, {"2020", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Table.DemoteHeaders(#"Changed Type"){0}[Column1])
in #"Added Custom"
然后只是 append 来自所有表的数据。
数据...获取数据..组合查询... Append...然后添加每个表
这将产生类似于
let Source = Table.Combine({Table1, Table2, Table3}) in Source
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.