python - 将 pandas DataFrame 导出到 googsheet 时缺少索引标题
问题描述
我需要将使用数据框制作的数据透视表放在 googlesheet 中。当我尝试在 jupyter 中运行它时,它应该显示:
但是当我将数据导出到 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)
我检查了很多教程,但没有找到解决方案。
解决方案
对于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 – 复制数据帧索引(支持多索引)。
推荐阅读
- java - 检测Android中特定变量中的值何时发生变化
- python - Doc2Vec:从 ConcatenatedDocvecs 推断出最相似的向量
- ruby-on-rails - 连接表中的 Factory Bot HABTM nil 值
- html - 如何以渐变样式为我的列表制作带有自定义项目符号的边框?
- python - 返回每组具有最高值的列表
- android - 我可以将 material-components-android 与 React-Native 一起使用吗?
- php - 没有错误字段的文件实体导致 Burzum/Filestorage 将文件视为不存在?
- npm - 同时 npm 和 React Js
- typescript - redux 动作类型的联合仍然允许未知类型
- php - 外键数据库插入问题