[英]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 ![]() |
b1 ![]() |
Green![]() |
a2 ![]() |
b2 ![]() |
Green![]() |
a3 ![]() |
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 ![]() |
Green![]() |
a2 ![]() |
Green![]() |
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 ![]() |
Blue![]() |
a1 ![]() |
b1 ![]() |
Green![]() |
a2 ![]() |
b2 ![]() |
Green![]() |
a3 ![]() |
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)
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'}]
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.当以上几点反映到您的脚本中时,它变成如下。
In this modified script, df = pd.DataFrame(final_worksheet.get_all_records())
is modified.在这个修改后的脚本中,
df = pd.DataFrame(final_worksheet.get_all_records())
被修改了。
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 modified script, the row you want to insert is put using the method of insert_rows()
.在这个修改后的脚本中,您要插入的行是使用
insert_rows()
的方法放置的。
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')
[['John', 99, 'Blue']]
can be directly inserted to the row 3.[['John', 99, 'Blue']]
的值直接插入到第 3 行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.