0
votes

The list of dataframes

df_list = [summary, df3, df4, df5, df6]

Names of excel sheets where the output has to be written

Names = ['Liability','Expenses','Income','Asset']

writer = pd.ExcelWriter(r"E:\GL\2040001.xlsx", engine='xlsxwriter')

If a dataframe has output then the file has to be written to excel

for i in df_list: if i.empty is False: for j in Names: i.to_excel(writer, sheet_name = j)

I want the files which are not empty to be written with its corresponding dataframe

1

1 Answers

0
votes

You just need to check if the dataframe is empty. If it is, you do nothing. If it is not, you write to Excel:

df_list = [summary, df3, df4, df5, df6]
names = ['Liability','Expenses','Income','Asset']
writer = pd.ExcelWriter(r"E:\GL\2040001.xlsx", engine='xlsxwriter')
for df, name in zip(df_list, names):
    if not df.is_empty:
        df.to_excel(writer, sheet_name = name)

Note that variable names in Python are preferred to be lowercase. Also, try not to use absolute path but use relative ones.