使用列值命名 excel 文件 + python 中这些值的别名

Naming excel files using column values + aliases for those values in python

DF 看起来像这样并扩展了数千行(即 'Type' 和 'Name' 的每种可能组合)

| total |  big  |  med  | small|   Type   |   Name   |
|:-----:|:-----:|:-----:|:----:|:--------:|:--------:| 
|   5   |   4   |   0   |   1  |   Pig    |   John   |
|   6   |   0   |   3   |   3  |  Horse   |   Mike   | 
|   5   |   2   |   3   |   0  |   Cow    |   Rick   |
|   5   |   2   |   3   |   0  |   Horse  |   Rick   |
|   5   |   2   |   3   |   0  |   Cow    |   John   |
|   5   |   2   |   3   |   0  |   Pig    |   Mike   |

我按 'Type' 和 'Name' 对数据框进行了分组。

| total |  big  |  med  | small|   Type   |   Name   |
|:-----:|:-----:|:-----:|:----:|:--------:|:--------:| 
|   5   |   4   |   0   |   1  |   Pig    |   John   |
|   6   |   0   |   3   |   3  |   Pig    |   John   | 
|   5   |   2   |   3   |   0  |   Pig    |   John   |
|   5   |   2   |   3   |   0  |   Pig    |   John   |

然后运行分别作用于每个分组数据帧。

for idx, df in data.groupby(['Type', 'Name']):
     function_1(df)
     function_2(df)

    with pd.ExcelWriter(f"{'_'.join(idx)}.xlsx") as writer:
        table_1.to_excel(writer, sheet_name='Table 1', index=False)
        table_2.to_excel(writer, sheet_name='Table 2', index=False)

结果文件名出来了:

"Pig_John.xlsx"

我想添加别名来分别替换每个 'Type' 和 'Name',如下所示。

Aliases: 

Pig = Type1
Horse = Type2
Cow = Type3
John = Name1
Mike = Name2
Rick = Name3

Example Result:

Pig_John.xlsx = Type1_Name1.xlsx
Horse_Rick.xlsx = Type2_Name3.xlsx

你可以创建一个字典,然后调用字典的键和值,创建一个新的 idx 每个循环 idx = (dct[idx[0]], dct[idx[1]]):

dct = {'Pig' : 'Type1',
'Horse' : 'Type2',
'Cow' : 'Type3',
'John' : 'Name1',
'Mike' : 'Name2',
'Rick' : 'Name3'}

df=d.copy()
for idx, d in df.groupby(['Type', 'Name']):
    idx = (dct[idx[0]], dct[idx[1]])
    print(f"{'_'.join(idx)}.xlsx")

Out[1]:
Type3_Name1.xlsx
Type3_Name3.xlsx
Type2_Name2.xlsx
Type2_Name3.xlsx
Type1_Name1.xlsx
Type1_Name2.xlsx