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.

Output to spreadsheet without filling in row headers until header name changes?

See original GitHub issue

Starting with the following dictionary:

    test_dict = {'header1_1': {'header2_1': {'header3_1': {'header4_1': ['322.5', 330.0, -0.28],
        'header4_2': ['322.5', 332.5, -0.26]},
       'header3_2': {'header4_1': ['285.0', 277.5, -0.09],
        'header4_2': ['287.5', 277.5, -0.12]}},
      'header2_2': {'header3_1': {'header4_1': ['345.0', 357.5, -0.14],
        'header4_2': ['345.0', 362.5, -0.14]},
       'header3_2': {'header4_1': ['257.5', 245.0, -0.1],
        'header4_2': ['257.5', 240.0, -0.08]}}}}

I want the headers in the index, so I reform the dictionary:

    reformed_dict = {}
    for outerKey, innerDict in test_dict.items():
        for innerKey, innerDict2 in innerDict.items():
            for innerKey2, innerDict3 in innerDict2.items():
                for innerKey3, values in innerDict3.items():
                    reformed_dict[(outerKey,
                            innerKey, innerKey2, innerKey3)] = values

And assign column names to the headers:

    keys = reformed_dict.keys()
    values = reformed_dict.values()
    index = pd.MultiIndex.from_tuples(keys, names=["H1", "H2", "H3", "H4"])
    df = pd.DataFrame(data=values, index=index)

That gets to a dataframe that looks like this: vDQ5R

Issue: When I write this dataframe to Google Sheets using gspread-pandas:

    s.open_sheet('test')
    Spread.df_to_sheet(s, df, index=True, headers=True, start='A8', replace=False) 

The table looks like this: midf_1

Would it be possible to manipulate the dataframe to get written to spreadsheet like this: midf_2

Issue Analytics

  • State:open
  • Created 10 months ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
aiguofercommented, Dec 10, 2022

I just pushed some changes to reset the merged cells before making the request to merge again… this should solve that.

One thing to note is that when you’re updating the data with less rows, the rest of the rows that were previously there are not cleared out. I ended up adding a replace flag to df_to_sheet to ensure there were no issues with lingering data. However, that does resize the spreadsheet to only the cells needed to fit the data.

1reaction
aiguofercommented, Dec 9, 2022

Thanks for pointing that out! I might do a little more testing to ensure at least the merged indexes update correctly on a second run with an updated DF

Read more comments on GitHub >

github_iconTop Results From Across the Web

Turn Excel table headers on or off - Microsoft Support
When you create an Excel table, a table Header Row is automatically added as the first row of the table, but you have...
Read more >
Print headings, gridlines, formulas, and more - Microsoft Support
Training: Print gridlines and, for multiple page worksheets, print row or column headers or labels (also called print titles) in the first row...
Read more >
Repeat Excel header rows on every page - Ablebits
Open the worksheet that you want to print with the row and column headings. · Go to the Sheet Options group on the...
Read more >
Page Breaks - repeat header row in Excel by Chris Menard
If you want your pages in Excel to have the header row at the top of each printed page, go to Page Layout,...
Read more >
Use the Column Header to Retrieve Values from an Excel Table
This function looks for the value in C$7 in the headers row of the table. We use a relative column reference (C) so...
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