简体   繁体   English

Python/Gspread 如何在创建工作表时添加列名?

[英]Python/Gspread How can I add column names while creating a worksheet?

#For creating a new-sheet
sheets.add_worksheet(title=sheetName, rows=df.shape[0], cols=df.shape[1])

#For appending
values = df.values.tolist()
sheets.values_append(sheetName, {'valueInputOption': 'USER_ENTERED'}, {'values': values})

It creates a new worksheet but there are no column names in it only the values.它创建了一个新工作表,但其中没有列名,只有值。

In your script, how about the following modification?在你的脚本中,如何进行以下修改?

From:从:

values = df.values.tolist()

To:到:

values = [df.columns.values.tolist()] + df.values.tolist()
  • By this, values has the data including the header row.至此, values的数据包括 header 行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM