Skip to content

Instantly share code, notes, and snippets.

@VildMedPap
Created May 7, 2021 12:28
Show Gist options
  • Save VildMedPap/38f366e3b4659d65cc83f26ca6fbc2fe to your computer and use it in GitHub Desktop.
Save VildMedPap/38f366e3b4659d65cc83f26ca6fbc2fe to your computer and use it in GitHub Desktop.
Python: Fetch data from Google Sheet
import gspread
import pandas as pd
from oauth2client.service_account import ServiceAccountCredentials
creds = ServiceAccountCredentials.from_json_keyfile_name(
filename="credentials.json",
scopes=["https://www.googleapis.com/auth/drive"]
)
# Initialize client
client = gspread.authorize(creds)
# Fetch name of first spreadsheet file
spreadsheet_file = client.list_spreadsheet_files()[0]["name"]
# Open spreadsheet
sh = client.open(spreadsheet_file)
# Get all values from first worksheet
data = sh.worksheets()[0].get_all_values()
# Use first row as column names and all subsequent rows as data
df = pd.DataFrame(data[1:], columns=data[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment