Python 3.x 转换工作表API';batchGet';请求到数据帧中

Python 3.x 转换工作表API';batchGet';请求到数据帧中,python-3.x,pandas,google-sheets-api,Python 3.x,Pandas,Google Sheets Api,为了减少对Sheets API的API调用数量,避免可怕的“error 429”消息,我希望使用Sheets API的“batchGet”函数。我已将所有相关信息放入一个谷歌电子表格电子表格id,其中包含多个工作表范围。下一步是将此batchGet请求转换为数据帧 这是我的密码。。。如果有人能为下一步的工作提供指导,让这项工作进入一个新的阶段,那就太好了 from googleapiclient.discovery import build from google_auth_oauthlib.f

为了减少对Sheets API的API调用数量,避免可怕的“error 429”消息,我希望使用Sheets API的“batchGet”函数。我已将所有相关信息放入一个谷歌电子表格
电子表格id
,其中包含多个工作表
范围
。下一步是将此batchGet请求转换为数据帧

这是我的密码。。。如果有人能为下一步的工作提供指导,让这项工作进入一个新的阶段,那就太好了

from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from oauth2client.service_account import ServiceAccountCredentials
import pandas as pd


SCOPES = [ 'https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/spreadsheets']

credentials = ServiceAccountCredentials.from_json_keyfile_name('creds.json', SCOPES)

service = discovery.build('sheets', 'v4', credentials=credentials)

# The ID of the spreadsheet to retrieve data from.
spreadsheet_id = 'my_spreadheet_id'  # TODO: Update placeholder value.

# The A1 notation of the values to retrieve.
ranges = ['2016_IGA!A2:BD',  '2017_IGA!A2:BD',  '2018_IGA!A2:BD',  '2019_IGA!A2:BD',  '2020_IGA!A2:BD',
'2016_Coles!A2:BD',  '2017_Coles!A2:BD',  '2018_Coles!A2:BD',  '2019_Coles!A2:BD',  '2020_Coles!A2:BD',                          # TODO: Update placeholder value.
'2016_WW!A2:BD',  '2017_WW!A2:BD',  '2018_WW!A2:BD',  '2019_WW!A2:BD',  '2020_WW!A2:BD', 
'2018_Aldi!A2:BD',  '2019_Aldi!A2:BD',  '2020_Aldi!A2:BD']

value_render_option = 'FORMATTED_VALUE'  

request = service.spreadsheets().values().batchGet(spreadsheetId=spreadsheet_id, ranges=ranges, valueRenderOption=value_render_option)
response = request.execute()

您必须从响应中获取
,然后从结果列表中创建
数据帧

sheet_values=response.get('values',[])
#可选:执行任何数据清理/争用操作(日期/货币转换)
#使用提取的值创建数据帧
df_sheet=DataFrame(sheet_值,列=['A','B','C'])

您必须从响应中获取
值,然后从结果列表中创建
数据帧

sheet_values=response.get('values',[])
#可选:执行任何数据清理/争用操作(日期/货币转换)
#使用提取的值创建数据帧
df_sheet=DataFrame(sheet_值,列=['A','B','C'])

在@juan Morais评论的基础上,我自己做了一些修改,这里是最终的解决方案

from googleapiclient import discovery
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from oauth2client.service_account import ServiceAccountCredentials
import pandas as pd
from pandas.io.json import json_normalize


SCOPES = [ 'https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/spreadsheets']

credentials = ServiceAccountCredentials.from_json_keyfile_name('creds.json', SCOPES)

service = discovery.build('sheets', 'v4', credentials=credentials)

# The ID of the spreadsheet to retrieve data from.
spreadsheet_id = 'my_spreadheet_id' 

# The A1 notation of the values to retrieve.
ranges = ['2016_IGA!A2:Be',  '2017_IGA!A2:Be',  '2018_IGA!A2:Be',  '2019_IGA!A2:Be',  '2020_IGA!A2:Be',
'2016_Coles!A2:Be',  '2017_Coles!A2:Be',  '2018_Coles!A2:Be',  '2019_Coles!A2:Be',  '2020_Coles!A2:Be',                          # TODO: Update placeholder value.
'2016_WW!A2:Be',  '2017_WW!A2:Be',  '2018_WW!A2:Be',  '2019_WW!A2:Be',  '2020_WW!A2:Be', 
'2018_Aldi!A2:Be',  '2019_Aldi!A2:Be',  '2020_Aldi!A2:Be']

value_render_option = 'FORMATTED_VALUE'  

request = service.spreadsheets().values().batchGet(spreadsheetId=spreadsheet_id, ranges=ranges, valueRenderOption=value_render_option,majorDimension='ROWS')
response = request.execute()

sheet_values = response.get('valueRanges', [])

df = json_normalize(sheet_values, sep = ",",record_path='values')

基于@juan Morais的评论和我自己的一些改编,这里是最终的解决方案

from googleapiclient import discovery
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from oauth2client.service_account import ServiceAccountCredentials
import pandas as pd
from pandas.io.json import json_normalize


SCOPES = [ 'https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/spreadsheets']

credentials = ServiceAccountCredentials.from_json_keyfile_name('creds.json', SCOPES)

service = discovery.build('sheets', 'v4', credentials=credentials)

# The ID of the spreadsheet to retrieve data from.
spreadsheet_id = 'my_spreadheet_id' 

# The A1 notation of the values to retrieve.
ranges = ['2016_IGA!A2:Be',  '2017_IGA!A2:Be',  '2018_IGA!A2:Be',  '2019_IGA!A2:Be',  '2020_IGA!A2:Be',
'2016_Coles!A2:Be',  '2017_Coles!A2:Be',  '2018_Coles!A2:Be',  '2019_Coles!A2:Be',  '2020_Coles!A2:Be',                          # TODO: Update placeholder value.
'2016_WW!A2:Be',  '2017_WW!A2:Be',  '2018_WW!A2:Be',  '2019_WW!A2:Be',  '2020_WW!A2:Be', 
'2018_Aldi!A2:Be',  '2019_Aldi!A2:Be',  '2020_Aldi!A2:Be']

value_render_option = 'FORMATTED_VALUE'  

request = service.spreadsheets().values().batchGet(spreadsheetId=spreadsheet_id, ranges=ranges, valueRenderOption=value_render_option,majorDimension='ROWS')
response = request.execute()

sheet_values = response.get('valueRanges', [])

df = json_normalize(sheet_values, sep = ",",record_path='values')

谢谢Juan,但当我这样做时,我的df_表返回为空。在我的电子表格中,“值”是从公式中派生出来的值-这会有影响吗?我相信我在不得不实施GA时也遇到了这个问题。因为我必须进行数据清理,所以我最终将值附加到一个不同的列表中,解析结果非常好。我使用了官方提供的代码-如果有帮助,请告诉我!好的,我已经解决了这个问题。我需要实际拥有
sheet\u values=response.get('valueRanges',[])
,然后使用以下命令规范化此json响应
df=json\u规范化(sheet\u values,sep=“,”,record\u path='values')
工作完美,有趣!如果我将来发现这个问题,我一定会把它写下来。很高兴我能帮忙!谢谢Juan,但当我这样做时,我的df_表返回为空。在我的电子表格中,“值”是从公式中派生出来的值-这会有影响吗?我相信我在不得不实施GA时也遇到了这个问题。因为我必须进行数据清理,所以我最终将值附加到一个不同的列表中,解析结果非常好。我使用了官方提供的代码-如果有帮助,请告诉我!好的,我已经解决了这个问题。我需要实际拥有
sheet\u values=response.get('valueRanges',[])
,然后使用以下命令规范化此json响应
df=json\u规范化(sheet\u values,sep=“,”,record\u path='values')
工作完美,有趣!如果我将来发现这个问题,我一定会把它写下来。很高兴我能帮忙!为了正确理解您的问题,您能否提供您想要的样本电子表格和样本输出?当然,请从中删除您的个人信息。为了正确理解您的问题,您能否提供您想要的样本电子表格和样本输出?当然,请从中删除您的个人信息。