0

I'm trying to export a list of Pandas data frames to Excel files using a generator expression. However nothing is exported once the script has finished executing. It works if I use a for loop, but not using a generator expression. I'm really interested in knowing how it could work, but also why, thanks in advance.

This doesn't work:

def process_pandas_dfs():
    (df.to_excel('df_name.xlsx') for df in list_of_dfs)

However this does:

def process_pandas_dfs():
    for df in list_of_dfs:
        df.to_excel('df_name.xlsx')
  • I'd suggest you to read on [Understanding generators in Python](https://stackoverflow.com/questions/1756096/understanding-generators-in-python) or related to understand why this expression is not being evaluated. Also list/generator comprehensions don't make any sense here. A list-comp is useful for creating lists, not as a means to merely write more compact code – yatu Aug 28 '19 at 09:41
  • @yatu if list_of_dfs was created as a generator and then iteratively output to Excel files, then each df wouldn't need to be kept in memory? –  Aug 28 '19 at 09:56
  • Yes that is correct, but for that use a generator function, not a gen-com. But indeed you avoid keeping them all in memory – yatu Aug 28 '19 at 10:04

1 Answers1

0

generator expressions are not evaluated during definition, they create a iterable generator object. Use a list comprehension instead:

[df.to_excel('df_name.xlsx') for df in list_of_dfs]

Though as Yatu pointed out, the for loop would be the appropriate method of executing this method