Index headers are missing when export pandas DataFrame to googsheet

Viewed 550

I need to put pivot table made with data frame in googlesheet. When i try to run it in jupyter it shows as it should:

screenshot from jupyter notebook

but when i export data to googlesheet, index headers are missing as you can see from the pic below:

This is how it looks when i export in in googlesheet

This is part of my code which is responsible for create dataframe and export it in googlesheet. i'm using pygsheets library to access googlesheets.

df1 = wks.get_as_df()

df1 = df1.apply(pd.to_numeric, errors='ignore')

df2 = pd.pivot_table(df1, index =['SSPU','Color'], columns = "Size",values='US 全库存',fill_value=0, aggfunc = sum, margins=True)

print(df2)

I checked many tutorials but didn't find a solution.

1 Answers

For the set_dataframe() function to write to a google sheet, set copy_index=True The default is copy_index=False per the docs. I have never used this library before, but hopefully that works for you.

set_dataframe(df, start, copy_index=False, copy_head=True, extend=False, fit=False, escape_formulae=False, **kwargs)

https://pygsheets.readthedocs.io/en/stable/worksheet.html

Parameters:

copy_index – Copy data frame index (multi index supported).

Related