我有一个数据框,我想根据“名称”列中的唯一字符串将其解析为单个文件。我可以用一个简单的函数来做到这一点,比如:
f= lambda x: x.to_excel(os.getcwd()+'\\{}.xlsx'.format(x.name), index= False)
df.groupby('names').apply(f)
需要注意的是,我想用条件格式写出这些文件。我已经能够在以下块中使用 ExcelWriter 实现所需的格式:
writer= pd.ExcelWriter('Test.xlsx', engine='xlsxwriter' )
df.to_excel(writer, sheet_name='Sheet1', index=False)
workbook= writer.book
worksheet= writer.sheets['Sheet1']
format1= workbook.add_format({'font_color':'#ec5a24'})
format2= workbook.add_format({'bg_color':'#4b4b4b','font_color':'#ffde00'})
format3= workbook.add_format({'font_color':'#86b05b'})
worksheet.conditional_format('A2:AM10000',{'type':'formula','criteria':'=INDIRECT("E"&ROW())= "High"', 'format':format1})
worksheet.conditional_format('A2:AM10000',{'type':'formula','criteria':'=INDIRECT("E"&ROW())= "Med"', 'format':format2})
worksheet.conditional_format('A2:AM10000',{'type':'formula','criteria':'=INDIRECT("E"&ROW())= "Normal"', 'format':format3})
workbook.close()
writer.save()
有没有一种方法可以让我将这两者结合起来,以便我的输出是许多文件(列名中的每个唯一名称一个),这些文件在 excel 文件中具有所需的条件格式?
每个请求的新代码:
def new_func():
f= lambda x: x.to_excel(os.getcwd()+'\\{}.xlsx'.format(x.name), index= False)
writer= pd.ExcelWriter(x.name, engine='xlsxwriter' )
df.to_excel(writer, sheet_name='Sheet1', index=False)
workbook= writer.book
worksheet= writer.sheets['Sheet1']
format1= workbook.add_format({'font_color':'#ec5a24'})
format2= workbook.add_format({'bg_color':'#4b4b4b','font_color':'#ffde00'})
format3= workbook.add_format({'font_color':'#86b05b'})
worksheet.conditional_format('A2:AM10000',{'type':'formula','criteria':'=INDIRECT("E"&ROW())= "High"', 'format':format1})
worksheet.conditional_format('A2:AM10000',{'type':'formula','criteria':'=INDIRECT("E"&ROW())= "Med"', 'format':format2})
worksheet.conditional_format('A2:AM10000',{'type':'formula','criteria':'=INDIRECT("E"&ROW())= "Low"', 'format':format3})
workbook.close()
writer.save()
df.groupby('names').apply(new_func())