KrisK
KrisK

Reputation: 31

How do I import data from Google Sheets to a Pandas Dataframe

I am trying to write a script to import data from Google Sheets into my python script as a dataframe to then analyse.

I have watched several videos but I can't seem to make it work more than once (I have several datasets I need to import).

The first dataset I imported seems to have worked using the following code:

import gspread

import pandas as pd

from google.oauth2.service_account import Credentials

scopes = ["https://www.googleapis.com/auth/spreadsheets"]

creds = Credentials.from_service_account_file("*api file name*", scopes=scopes)

client = gspread.authorize(creds)

sheets_id = "*google sheet id*"

sheet = client.open_by_key(sheets_id)

faculty_df = pd.DataFrame(worksheet.get_all_records())
faculty_df

When I try to run something similar to read in a second dataset from a different google sheet doc (obviously adapting it and changing variable names), it is still bringing through the first dataset.

I assume it is something around the worksheet.get_all_records() code when creating the dataframe, but I am unsure on how to adapt the code to make it work more than once.

Can anyone advise me on how to fix this? or is there a better way overall the get the data in?

Any help or advice at all would be greatly appreciated.

Thank you in advance.

Upvotes: 0

Views: 142

Answers (0)

Related Questions