简体   繁体   English

关于使用gspread检索数据和更新的问题

[英]Question on using gspread to retrieve data and update

I have a google spreadsheet that I want to retrieve: This is how the data appears on google sheet.我有一个要检索的谷歌电子表格:这就是数据在谷歌表格上的显示方式。 'Business' is the first header and the second row 'Name','Age','Fav color' are the second headers. 'Business' 是第一个 header,第二行 'Name','Age','Fav color' 是第二个标题。

Business商业
Name姓名 Age年龄 Fav color最喜欢的颜色
a1 a1 b1 b1 Green绿色的
a2 a2 b2 b2 Green绿色的
a3 a3 b3 b3 Green绿色的
import gspread
import pandas as pd
import gspread_dataframe as gd

worksheet = sh.get_worksheet(index[0])
df = pd.DataFrame(final_worksheet.get_all_records())

When I get the worksheet, the output shows up as:当我得到工作表时,output 显示为:

Business商业
Name姓名 Fav color最喜欢的颜色
a1 a1 Green绿色的
a2 a2 Green绿色的
a3 a3 Green绿色的

Which isn't what I am looking for.这不是我要找的。

Question:I am hoping that I can grab the dataframe with Name, Age, Fav color as column headers (without the Business header) and then allow me to insert some values so that the final output (on google spreadsheet) would be:问题:我希望我可以获取 dataframe 作为列标题(没有业务标题),然后允许我插入一些值,以便最终的 output(在谷歌电子表格上)将是:

Business商业
Name姓名 Age年龄 Fav color最喜欢的颜色
John约翰 99 99 Blue蓝色的
a1 a1 b1 b1 Green绿色的
a2 a2 b2 b2 Green绿色的
a3 a3 b3 b3 Green绿色的

Here is the code that I used to update my spreadsheet:这是我用来更新电子表格的代码:

final_updated = updated.append(final_output)
gd.set_with_dataframe(final_worksheet, final_updated)

Modification points:修改点:

  • When the values from your sheet are retrieved with final_worksheet.get_all_records() , the following value is returned当使用final_worksheet.get_all_records()检索工作表中的值时,将返回以下值

     [{'': 'Fav color', 'Business': 'Name'}, {'': 'Green', 'Business': 'a1'}, {'': 'Green', 'Business': 'a2'}, {'': 'Green', 'Business': 'a3'}]
    • I thought that this might be the reason of your issue.我认为这可能是您的问题的原因。
    • In this case, how about creating the dataframe from the slice retrieved with get_all_values() ?在这种情况下,如何从使用get_all_values()检索到的切片创建 dataframe ?
  • About I am hoping that I can grab the dataframe with Name, Age, Fav color as column headers (without the Business header) and then allow me to insert some values so that the final output (on google spreadsheet) would be: , in this case, how about using the method of insert_rows() ?关于I am hoping that I can grab the dataframe with Name, Age, Fav color as column headers (without the Business header) and then allow me to insert some values so that the final output (on google spreadsheet) would be:案例,如何使用insert_rows()的方法? I thought that in this case, the script might be simpler.我认为在这种情况下,脚本可能会更简单。

  • In your script, worksheet is declared.在您的脚本中,声明了worksheet But it seems that final_worksheet is not declared.但似乎final_worksheet没有声明。

When above points are reflecvted to your script, it becomes as follows.当以上几点反映到您的脚本中时,它变成如下。

Modified script 1:修改脚本1:

In this modified script, df = pd.DataFrame(final_worksheet.get_all_records()) is modified.在这个修改后的脚本中, df = pd.DataFrame(final_worksheet.get_all_records())被修改了。

From: 从:
 df = pd.DataFrame(final_worksheet.get_all_records())
To: 至:
worksheet = sh.get_worksheet(index[0])
worksheet.insert_rows([['John', 99, 'Blue']], row=3, value_input_option='USER_ENTERED')

Modified script 2:修改脚本2:

In this modified script, the row you want to insert is put using the method of insert_rows() .在这个修改后的脚本中,您要插入的行是使用insert_rows()的方法放置的。

From: 从:
 worksheet = sh.get_worksheet(index[0]) df = pd.DataFrame(final_worksheet.get_all_records())
To: 至:
 worksheet = sh.get_worksheet(index[0]) worksheet.insert_rows([['John', 99, 'Blue']], row=3, value_input_option='USER_ENTERED')
  • In this case, the value of [['John', 99, 'Blue']] can be directly inserted to the row 3.在这种情况下,可以将[['John', 99, 'Blue']]的值直接插入到第 3 行。

References:参考:

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

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