【问题标题】:How can we create separate excel sheets using python pandas?我们如何使用 python pandas 创建单独的 excel 表?
【发布时间】:2021-10-28 15:49:18
【问题描述】:

例如:我有一个 Excel 文件作为输入,其中包含 50K 条员工记录。 员工在不同的公司 A、B 和 C 工作。

我们如何根据公司过滤这个输入的Excel文件并创建新的输出Excel文件。 其中包含针对公司 A、B 和 C 的单独工作表。

在输出的 excel 文件中,工作表 A 应该只有在 A 公司工作的员工。 表 B 应该只有在 B 公司工作的员工。 表 C 应该只有在 C 公司工作的员工。

【问题讨论】:

  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: python excel pandas automation


【解决方案1】:

您可以尝试以下方法:

df=pd.read_excel('your_main_file.xlsx')

writer = pd.ExcelWriter('output.xlsx', engine = 'xlsxwriter')
for i in set(df['Company']):
    temp = df[df.Company == i]
    temp.to_excel(writer, sheet_name='Company_'+str(i))

writer.save()
writer.close()  

【讨论】:

  • 我相信他需要一个包含多个工作表的 excel 文件,您可以将其作为第二个参数添加到 to_excel。
  • 感谢您的帮助。但我想要一个包含多张工作表的 excel 文件,正如@Mohammad 提到的那样
  • @AbhishekChoubey 更新的代码确实可以做到这一点
  • df=pd.read_excel('your_main_file.xlsx') writer = pd.ExcelWriter('output.xlsx', engine = 'xlsxwriter') for i in set(df['Company']) : temp = df[df.Company == i] temp.to_excel(writer, sheet_name='Company_'+str(i)) writer.save() writer.close() @IoaTzimas 非常感谢这个代码作为我的要求。
猜你喜欢
  • 1970-01-01
  • 2016-11-07
  • 1970-01-01
  • 1970-01-01
  • 2021-03-27
  • 2020-01-13
  • 1970-01-01
  • 2019-05-30
  • 2019-04-08
相关资源
最近更新 更多