Skip to content

Instantly share code, notes, and snippets.

@devmnj
Created October 21, 2024 12:40
Show Gist options
  • Save devmnj/d06e9944b79f40a8bb721731f8a95718 to your computer and use it in GitHub Desktop.
Save devmnj/d06e9944b79f40a8bb721731f8a95718 to your computer and use it in GitHub Desktop.
Load google sheet data into panda frame
# load data frame in google sheet
import gspread
from google.oauth2.service_account import Credentials
from google.auth import default
# Define the scope of access you need
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
creds, _ = default()
client = gspread.authorize(creds)
spreadsheet = client.open('data_sheet')
worksheet = spreadsheet.worksheet('data')
data = worksheet.get_all_values()
df = pd.DataFrame(data[1:], columns=data[0])
# Convert relevant columns to numeric types
df['PE_ratio'] = pd.to_numeric(df['PE_ratio'], errors='coerce')
df['Sharpe_ratio'] = pd.to_numeric(df['Sharpe_ratio'], errors='coerce')
df['Alpha'] = pd.to_numeric(df['Alpha'], errors='coerce')
df['Beta'] = pd.to_numeric(df['Beta'], errors='coerce')
df.head()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment