首页 > 解决方案 > 将 pandas DataFrame 导出到 googsheet 时缺少索引标题

问题描述

我需要将使用数据框制作的数据透视表放在 googlesheet 中。当我尝试在 jupyter 中运行它时,它应该显示:

jupyter笔记本的截图

但是当我将数据导出到 googlesheet 时,索引标题丢失了,如下图所示:

这是我在 googlesheet 中导出时的样子

这是我的代码的一部分,它负责创建数据框并将其导出到 googlesheet 中。我正在使用 pygsheets 库来访问 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)

我检查了很多教程,但没有找到解决方案。

标签: pythonpandasdataframegoogle-sheetspivot-table

解决方案


对于set_dataframe()写入谷歌表的函数,设置copy_index=True默认值是copy_index=False每个文档。我以前从未使用过这个库,但希望它对你有用。

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

参数:

copy_index – 复制数据帧索引(支持多索引)。


推荐阅读