1

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

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

我想重新排列表格,使其看起来像这样,并为 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 |
+-----------+-------+------+-------------------+---------------+

对此有很多动人的部分,并且要使最终格式正确,有些棘手。

4

2 回答 2

2

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 
于 2020-02-14T22:42:45.557 回答
0

这就是我能够将 Yoben 的解决方案应用于 xlsx 文件中的每个选项卡,将它们附加在一起并将完整表写入 .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')
于 2020-02-16T15:35:27.327 回答