I have a number of dataframes that I am exporting into one Excel document.
My current code works great and does exactly what I want it to do but I would like to apply, starting at for column in Professional_Mask:
, to all of my dataframes. Instead of having to copy and past this code multiple times, is there a way I can turn this into a function?
I've been reading up in functions but this task I think requires more expertise than my current knowledge. Basic explanation of python functions
Here is my code for the first dataframe I would like to export.
writer = pd.ExcelWriter('Payroll Analysis.xlsx', engine='xlsxwriter')
Professional_Mask.to_excel(writer, index=False, sheet_name='Professional Emp All CC')
worksheet1 = writer.sheets['Professional Emp All CC']
worksheet1.freeze_panes(1, 3)
for column in Professional_Mask:
column_length = max(Professional_Mask[column].astype(str).map(len).max(), len(column))
col_idx = Professional_Mask.columns.get_loc(column)
writer.sheets['Professional Emp All CC'].set_column(col_idx, col_idx, column_length)
....
writer.save()
My other dataframes are titled 'Nursing Emp All CC', 'Professional Job By CC', etc...
Thank you!