2

I have a dict with 100s of panda dfs.

I want to loop through each df in the dict and dump it into excel, all on a single sheet, one after another with 1 blank row inbetween.

My attempt:

writer = pd.ExcelWriter('output.xlsx', engine='xlsxwriter')
workbook = writer.book

for key, values in dd.iteritems():
    df = dd[key]['chart_data']
    df.to_excel(writer, sheet_name='Sheet 1', index=False)

writer.save()
workbook.close()

I think it overwrites the dfs.

Any suggestions?

1
  • 1
    It is not that the df is overwritten, it's that you paste into the same part of the excel sheet every time. Thus you are physically overwriting the information in Excel. Assign the startrow in .toexcel(). By default it pastes from the top-left, which is not what you want here. Commented Nov 16, 2018 at 14:33

1 Answer 1

5

startrow sounds like your solution:

start_row = 0
for key, values in dd.iteritems():
    df = dd[key]['chart_data']
    df.to_excel(writer, sheet_name='Sheet 1', index=False, startrow=start_row)
    # Edited to respect your requirement for 1 blank row
    # after each df
    start_row = start_row + len(df) + 1 # or df.shape[0] et cetera

It simply picks which row to start dumping into. You may also want to specify startcol, which works on the same principle, but I think this works as-is.

Edit: another, perhaps better way is to concat. Something like:

df = pd.concat([dd[key]["chart_data"] for key, values in dd.iteritems()])
df.to_excel(...)

But that would only work if your df fits in memory.

Sign up to request clarification or add additional context in comments.

6 Comments

If it doesn't fit in memory, how come it is in dict already?
Because now the dict and the df must fit in memory @MuhammadAhmad
start_row = start_row + len(df)+1 to get the blank row between df dumps? Or start_row += len(df)+1 for a tiny, tiny gain in speed and memory.
I don't see any reason for a blank row between dumps and it wasn't in the question @Mark_Anderson. Although if the answer as written overwrites the last row of the previous df on each iteration then your edit would be mandatory. I don't think it does but I can't test right now
It's in the question, second sentence: "I want to loop through each df in the dict and dump it into excel, all on a single sheet, one after another with 1 blank row inbetween."
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.