我正在下面运行这段代码,
sh = gc.open_by_url('..........')
worksheet = sh.worksheet('Sheet1')
worksheet.update([df2.columns.values.tolist()] + df2.values.tolist()+df2.index.values.tolist())
df2 是一个多索引数据透视表,运行此代码仅写入值和列,但不包括 Google 表格上的索引。有没有办法包含索引并能够更改 Google 表格上的标题格式?
我遇到了类似的问题并使用配套库解决了这个问题gspread_pandas
gspread
和 google Sheets API 之间的交换本身不支持 MultiIndex(行或列)。
有没有办法包含索引并能够更改 Google 表格上的标题格式?
gspread_pandas.Spread.dt_to_sheet()
方法,其参数用于修改 MultiIndex 行和列的处理方式。这包括一系列布尔标志
sh = gc.open_by_url('..........')
worksheet = sh.worksheet('Sheet1')
worksheet.update([df2.columns.values.tolist()] + df2.values.tolist()+df2.index.values.tolist())
# Import the gspread_pandas 'Spread' module
from gspread_pandas import Spread
# Using the same credentials used for gspread.authorize(credentials)
sh = Spread(spread='https://...spreadsheet_url...', creds=credentials)
sh.df_to_sheets(df=df2, # Your dataframe
sheet='Sheet 1', # Target sheet name
start='A1', # First cell to place the data in
index=True, # Show the index
headers=True, # Show the columns
merge_headers=True, # Merge the cells of multiindex headers
merge_index=True, # Merge the rows of the muliindex index
freeze_headers=True, # Freeze the header rows
)