简体   繁体   中英

Python ExcelWriter Nested Loop to Create Multi-sheet Workbooks

I wrote some code that loops through.csv files in a directory, grabs only the rows == a particular agency name, and then outputs a workbook with multiple sheets for the agency, one sheet per file in the directory. This code works perfectly and is shown below

agency_name = 'Example Agency'
writer = pd.ExcelWriter(f'path/{agency_name } HoH Errors 2022.xlsx', engine='xlsxwriter')
for filename in os.listdir(input_directory):
     p = os.path.join(input_directory,filename)
     df = pd.read_csv(p)
     df = df[df['Agency Name'] == agency_name]
     if df.empty == True:
         continue
     df.to_excel(writer,filename.replace('.csv',''), index=False)
writer.save()

My issue is that when I then try to nest this code into another for loop that loops through hundreds of agencies, the workbooks are saved with the correct name to the correct directory, but they are all blank - none of the data is being saved to the workbook. Here's the code

agency_name = pd.DataFrame(data = {'Agency Name':['Example 1','Example 2','Example 3']})
for i in range(len(agency_name)):
     writer = pd.ExcelWriter(f"path/{agency_name.iloc[i,0]} HoH Errors 2022.xlsx", engine='xlsxwriter')
     for filename in os.listdir(input_directory):
         p = os.path.join(input_directory,filename)
         df = pd.read_csv(p)
         df = df[df['Agency Name'] == agency_name.iloc[i,0]]
         if df.empty == True:
             continue
         df.to_excel(writer,filename.replace('.csv',''), index=False)
     writer.save()

While troubleshooting I have found that the loop is still producing the corrects dfs, the issue is that these dfs are not being saved to the workbook as worksheets. Again, the workbooks are saved in the right place with the right names, but none of them contain any data. Does anyone have any guidance? I have not used ExcelWriter extensively. Thanks!

I was able to solve the issue by adding these lines:

if df.empty == True:
        if filename == 'Last_File_in_Directory.csv':
            writer.save()
            continue
        else:
            continue

As things stood, if the last file in the directory didn't have any records matching the Agency then the file wouldn't save/close.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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