Pandas - 旋转和重新排列 Table 多个标签相同 Header

Pandas - Pivot and Rearrange Table With Multiple Labels in Same Header

我有一个 xlsx 文件,其中包含多年数据的选项卡。每个选项卡都包含一个包含许多列的 table,table 的结构如下:

+-----------+-------+-------------------------+----------------------+
|   City    | State | Number of Drivers, 2019 | Number of Cars, 2019 |
+-----------+-------+-------------------------+----------------------+
| LA        | CA    |                     123 |                 10.0 |
| San Diego | CA    |                     456 |                 2345 |
+-----------+-------+-------------------------+----------------------+

我想将 table 重新排列成这样,并为 xlsx 中的每个选项卡执行此操作:

+-----------+-------+------+-------------------+---------------+
|   City    | State | Year |   Measure Name    | Measure Value |
+-----------+-------+------+-------------------+---------------+
| LA        | CA    | 2019 | Number of Drivers |           123 |
| San Diego | CA    | 2019 | Number of Drivers |           456 |
| LA        | CA    | 2019 | Number of Cars    |            10 |
| San Diego | CA    | 2019 | Number of Cars    |          2345 |
+-----------+-------+------+-------------------+---------------+

这其中有很多变动的部分,要使最终的格式正确也有点棘手。

我们做 melt 然后 joinstr.split

s=df.melt(['City','State'])
s=s.join(s.variable.str.split(',',expand=True))
Out[120]: 
       City State              variable   value                0     1
0        LA    CA  NumberofDrivers,2019   123.0  NumberofDrivers  2019
1  SanDiego    CA  NumberofDrivers,2019   456.0  NumberofDrivers  2019
2        LA    CA     NumberofCars,2019    10.0     NumberofCars  2019
3  SanDiego    CA     NumberofCars,2019  2345.0     NumberofCars  2019

# if you need change the name adding .rename(columns={}) at the end 

这就是我如何将 Yoben 的解决方案应用于 xlsx 文件中的每个选项卡,将它们附加在一起并将完整的 table 写入 .csv:

sheets_dict = pd.read_excel(r'file.xlsx', sheet_name=None)

full_table = pd.DataFrame()
for name, sheet in sheets_dict.items():
    sheet['sheet'] = name
    sheet = sheet.melt(['City','State'])
    sheet = sheet.join(sheet.variable.str.split(',' , expand=True))
    full_table = full_table.append(sheet)


full_table.reset_index(inplace=True, drop=True)


full_table.to_csv('Full Table.csv')