简体   繁体   English

每个唯一值的 Excel 文件(多张)的数据帧

[英]Dataframes to Excel file (multiple sheets) per unique value

I have three different dataframes which all contain a column with certain IDs.我有三个不同的数据框,它们都包含具有特定 ID 的列。

DF_1 DF_1

DF_1

DF_2 DF_2

DF_2

DF_3 DF_3

DF_3

What I am trying to achieve is to create an Excel sheet with the ID as its name with the dataframes as the sheets 'DF_1, DF_2, DF_3' per unique value.我想要实现的是创建一个 Excel 工作表,其 ID 为其名称,数据帧为每个唯一值的工作表“DF_1、DF_2、DF_3”。 So '1.xlsx' should contain three sheets (the dataframes) with only the records that are of associated with that ID.所以“1.xlsx”应该包含三张表(数据框),其中只有与该 ID 相关联的记录。 The thing I get stuck at is either getting the multiple sheets or only the corresponding values per unique value.我遇到的问题是要么获取多张纸,要么只获取每个唯一值的对应值。

for name, r in df_1.groupby("ID"):
   r.groupby("ID").to_excel(f'{name}.xlsx', index=False)

This piece of code gives me the correct output, but only for df_1.这段代码给了我正确的 output,但仅适用于 df_1。 I get 5 Excel files with the corresponding rows per ID, but only one sheet, namely for df_1.我得到 5 个 Excel 文件,每个 ID 都有相应的行,但只有一张,即 df_1。 I can't figure out how to include df_2 and df_3 per ID.我不知道如何在每个 ID 中包含 df_2 和 df_3。 When I try to use the following piece of code with nested loops, I get all the rows instead of per unique value:当我尝试将以下代码与嵌套循环一起使用时,我得到所有行而不是每个唯一值:

writer = pd.ExcelWriter(f'{name}.xlsx')
r.to_excel(writer, sheet_name=f'{name}_df1')
r.to_excel(writer, sheet_name=f'{name}_df2')
r.to_excel(writer, sheet_name=f'{name}_df3')
writer.save()

There is more data transformation going on before this part, and the final dataframes are the once that are needed eventually.在这部分之前还有更多的数据转换,最终的数据帧是最终需要的一次。 Frankly, I have no idea how to fix this or how to achieve this.坦率地说,我不知道如何解决这个问题或如何实现这一点。 Hopefully, someone has some insightful comments.希望有人有一些有见地的评论。

Can you try the following:您可以尝试以下方法:

unique_ids = df_1['ID'].unique()
for name in unique_ids:
    writer = pd.ExcelWriter(f'{name}.xlsx')

    r1 = df_1[df_1['ID'].eq(name)]
    r1.to_excel(writer, sheet_name=f'{name}_df1')

    r2 = df_2[df_2['ID'].eq(name)]
    r2.to_excel(writer, sheet_name=f'{name}_df2')

    r3 = df_3[df_3['ID'].eq(name)]
    r.to_excel(writer, sheet_name=f'{name}_df3')

    writer.save()    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM