Pandas:分析来自

2024-06-28 20:10:43 发布

您现在位置:Python中文网/ 问答频道 /正文

excel工作表中的数据存储如下:

   Area     |          Product1     |      Product2        |      Product3
            |      sales|sales.Value|   sales |sales.Value |  sales |sales.Value
  Location1 |    20     | 20000     |      25 |  10000     |   200  | 100
  Location2 |    30     | 30000     |      3  | 12300      |   213  | 10

产品名称由两行的两个单元格合并而成,即“销售数量”和“销售价值”,每个单元格对应于给定月份的1000个左右的区域。同样,过去5年每个月都有单独的文件。此外,新产品在不同的月份被添加和删除。因此,不同的月份文件可能如下所示:

   Area     |          Product1     |      Product4        |      Product3

论坛能推荐使用熊猫阅读这些数据的最佳方式吗? 我不能使用索引,因为每个月的产品列都不同

理想情况下,我希望将上述初始格式转换为:

 Area      | Product1.sales|Product1.sales.Value| Product2.sales |Product2.sales.Value | 
 Location1 | 20            | 20000              | 25             | 10000               |  
 Location2 | 30            | 30000              | 3              | 12300               | 

import pandas as pd
xl_file = read_excel("file path", skiprow=2, sheetname=0)
/* since the first two rows are always blank */


                  0            1        2               3                      4
      0          NaN          NaN      NaN       Auto loan                    NaN
      1  Branch Code  Branch Name   Region  No of accounts  Portfolio Outstanding
      2         3000       Name1  Central               0                      0
      3         3001       Name2  Central               0                      0

我想把它转换成Auto loan.No of accountAuto loan.Portfolio Outstanding作为标题。


Tags: 文件数据autovalueareananexcelsales