Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I Export Pandas DataFrame to Google Sheets using Python?

I managed to read data from a Google Sheet file using this method:

# ACCES GOOGLE SHEET
googleSheetId = 'myGoogleSheetId'
workSheetName = 'mySheetName'
URL = 'https://docs.google.com/spreadsheets/d/{0}/gviz/tq?tqx=out:csv&sheet={1}'.format(
    googleSheetId,
    workSheetName
)
df = pd.read_csv(URL)

However, after generating a pd.DataFrame that fetches info from the web using selenium, I need to append that data to the Google Sheet.

Question: Do you know a way to export that DataFrame to Google Sheets?

like image 496
Mihnea-Octavian Manolache Avatar asked Jul 15 '20 15:07

Mihnea-Octavian Manolache


People also ask

How do I export data from Python to Google Sheets?

Search for 'Google Drive API', enable it. Select Compute Engine service default, JSON, hit create. Open up the JSON file, share your spreadsheet with the "[email protected]" email listed. Save the JSON file wherever you're hosting your project, you'll need to load it in through Python later.

How do I export Panda DataFrame?

Exporting the DataFrame into a CSV filePandas DataFrame to_csv() function exports the DataFrame to CSV format. If a file argument is provided, the output will be the CSV file. Otherwise, the return value is a CSV format like string. sep: Specify a custom delimiter for the CSV output, the default is a comma.

How do I get data from Pandas DataFrame in Python?

Pandas DataFrame get() Method The get() method returns the specified column(s) from the DataFrame. If you specify only one column, the return value is a Pandas Series object. To specify more than one column, specify the columns inside an array. The result will be a new DataFrame object.


1 Answers

Yes, there is a module called "gspread". Just install it with pip and import it into your script.

Here you can find the documentation: https://gspread.readthedocs.io/en/latest/

In particular their section on Examples of gspread with pandas.

worksheet.update([dataframe.columns.values.tolist()] + dataframe.values.tolist())
like image 178
Daniel Avatar answered Sep 18 '22 06:09

Daniel