【问题标题】:Converting a Sheets API 'batchGet' request into a Pandas DataFrame将 Sheets API 'batchGet' 请求转换为 Pandas DataFrame
【发布时间】:2019-07-15 01:40:03
【问题描述】:

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

这是我的代码...如果有人可以就下一步将其导入 pandas df 提供指导,那就太好了。

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()

【问题讨论】:

  • 为了正确理解您的问题,您能否提供您想要的示例电子表格和示例输出?当然,请从他们那里删除您的个人信息。

标签: python-3.x pandas google-sheets-api


【解决方案1】:

您必须从响应中获取values,然后从结果列表中创建DataFrame

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

# Optional: Perform any data cleaning/wrangling operations (Date/currency conversion)

# Create a dataframe with the extracted values
df_sheet = DataFrame(sheet_values, columns=['A', 'B', 'C'])

【讨论】:

  • 感谢胡安,但是当我这样做时,我的 df_sheet 返回空。在我的电子表格中,“值”是从公式中得出的值 - 这会产生影响吗?
  • 我相信当我不得不在我这边实施 GA 时也遇到了这个问题。因为我必须进行数据清理,所以我最终将值附加到一个不同的列表中,它解析得很好。我使用了官方Google API Docs提供的代码-如果有帮助请告诉我!
  • 好的,我已经解决了这个问题。我需要实际拥有sheet_values = response.get('valueRanges', []),然后使用以下内容规范化这个 json 响应。 . df = json_normalize(sheet_values, sep = ",",record_path='values') 完美运行
  • 啊,有趣!如果我以后发现这个问题,我一定会写下来。很高兴我能提供帮助!
【解决方案2】:

在@juan Morais cmets 的基础上进行了一些我自己的改编,这是最终的解决方案..

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')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 2017-12-01
    • 2017-03-17
    • 2017-03-23
    • 2017-04-13
    • 2021-03-29
    相关资源
    最近更新 更多