Stephen
Stephen

Reputation: 467

Pandas: Iterate through a list of DataFrames and export each to excel sheets

Trying to teach myself coding to automate some tedious tasks at work. I apologize for any unintentional ignorance.

I have created Data Frames in pandas (python 3.x). I want to print each data frame to a different excel sheet. Here is what I have for 2 Data Frames, it works perfect but I want to scale it to loop through a list of data frames so that I can make it a bit more dynamic.

writer = pandas.ExcelWriter("MyData.xlsx", engine='xlsxwriter')
Data.to_excel(writer, sheet_name="Data")
ByBrand.to_excel(writer, sheet_name="ByBrand")
writer.save()

Easy enough, but when there are 50+ sheets that need to be created it will get tedious.

Here is what I tried, it did not work:

writer = pandas.ExcelWriter("MyData.xlsx", engine='xlsxwriter')
List = [Data , ByBrand]
for i in List:
        i.to_excel(writer, sheet_name= i)
writer.save()

I think the issue is that the sheet_name field must be a string because as-is it creates an error. But if I put sheet_name= "i", it only creates one sheet called "i" with the data from Data, but doesn't iterate to ByBrand. Also, the excel file would be a nightmare if the sheets weren't named to their corresponding data frame, so please no suggestions for things like numbered sheets.

Thank you so much in advance, this website has been invaluable for my journey into coding.

-Stephen

Upvotes: 3

Views: 5862

Answers (1)

unutbu
unutbu

Reputation: 879481

It is easier to go from the string 'Data' to the value Data than the other way around. You can use locals()['Data'] to access the value associated to the variable whose string name is 'Data':

import pandas as pd

writer = pd.ExcelWriter("MyData.xlsx", engine='xlsxwriter')
seq = ['Data', 'ByBrand']
for name in seq:
    df = locals()[name]
    df.to_excel(writer, sheet_name=name)
writer.save()

locals() returns a read-only dictionary containing the current scope's local variables. globals() returns a dictionary containing the current scope's global variables. (Thus, if Data and ByBrand are defined in the global namespace rather than the local namespace, use globals() instead of locals().)


Another option is to collect the DataFrames in a dict. Instead of creating a variable for each DataFrame, make one dict, and let the keys be the sheet names and the values be DataFrames:

import pandas as pd

dfs = dict()
dfs['Data'] = ...
dfs['ByBrand'] = ...

writer = pd.ExcelWriter("MyData.xlsx", engine='xlsxwriter')
for name, df in dfs.items():
    df.to_excel(writer, sheet_name=name)
writer.save()

I think this is preferable since it does not require introspection tools like locals() or globals(). This second approach just uses a dict the way dicts are intended to be used: mapping keys to values.

Upvotes: 5

Related Questions