question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ENH: #40231 does not allow writing multiple dataframes to a single sheet

See original GitHub issue

The problem

The following code works well, both in 1.4.0 and 1.2.5:

df = pd.DataFrame(["=1+7"])
with pd.ExcelWriter("test.xlsx",engine="openpyxl") as writer:
    df.to_excel(writer,sheet_name="Sheet2")
    pd.DataFrame(["=1+1"]).to_excel(writer,sheet_name="Sheet2",header=None,index=False,startcol=4,startrow=3)

however, the following code raises an error in 1.4.0, but works in 1.2.5 (it creates a new sheet and writes both dataframes to it):

pd.DataFrame(["=1+1"]).to_excel("test.xlsx")
df = pd.DataFrame(["=1+7"])
with pd.ExcelWriter("test.xlsx",engine="openpyxl",mode="a") as writer:
    df.to_excel(writer,sheet_name="Sheet2")
    pd.DataFrame(["=1+1"]).to_excel(writer,sheet_name="Sheet2",header=None,index=False,startcol=4,startrow=3)

Also other options cannot replicate the old behaviour.

Describe the solution you’d like

An added option to if_sheet_exists="write_to" that justs selects the sheet and writes into it, without first deleting its contents. This also allows to append a dataframe within a sheet.

EDIT: in #40230 this was also suggested behaviour, but it was not implemented:

Originally I considered adding a flag like overwrite=True to pd.ExcelWriter but I don’t think this would be sufficient to cover all the potential behaviours, so would suggest an enum along the lines of the if_exists option in df.to_sql, with the following options:

* new_sheet: Create a new sheet with a different name.

* overwrite_sheet: Delete the contents of the sheet, then write to it.

* overwrite_cells: Write directly to the named sheet without deleting the previous contents.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
feefladdercommented, Sep 17, 2021

This will be solved by #42222 which will be merged soon and out in 1.4.0 😃

2reactions
rhshadrachcommented, Aug 9, 2021

@simonjayhawkins - #42222 is adding back in the feature that was (unknowingly, I think) removed. If that’s regarded as an enhancement, how can this regression get fixed for e.g. 1.3.2?

That said, there was opposition to implementing the option in question back in the PR causing this regression. If there is still sufficient opposition to this, then it seems to me the regression should be closed as a won’t-fix.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pandas - Write multiple dataframes to single excel sheet
1 Answer 1 ... The first thing that jumps out to me is that you are changing the sheet name each time, by...
Read more >
How to Write Multiple Data Frames in an Excel Sheet
I have multiple data frames with same column names. I want to write them together to an excel sheet stacked vertically on top...
Read more >
Export Multiple Pandas DataFrames to a single Excel sheet
Have you ever wanted to export multiple pandas DataFrames to one Excel sheet ? In this video, I'll show you how to export...
Read more >
How to Write Pandas DataFrames to Multiple Excel Sheets?
In this article, we will see how to export different DataFrames to different excel sheets using python. Pandas provide a function called ...
Read more >
Example: Pandas Excel with multiple dataframes - XlsxWriter
An example of writing multiple dataframes to worksheets using Pandas and ... engine='xlsxwriter') # Write each dataframe to a different worksheet.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found