5

I followed the steps here and here but couldn't upload a pandas dataframe to google sheets.

First I tried the following code:

import gspread
from google.oauth2.service_account import Credentials

scope = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/drive']

credentials = Credentials.from_service_account_file('my_json_file_name.json', scopes=scope)

gc = gspread.authorize(credentials)

spreadsheet_key = '1FNMkcPz3aLCaWIbrC51lgJyuDFhe2KEixTX1lsdUjOY'
wks_name = 'Sheet1'
d2g.upload(df_qrt, spreadsheet_key, wks_name, credentials=credentials, row_names=True)

The above code returns an error message like this: AttributeError: module 'df2gspread' has no attribute 'upload' which doesn't make sense since df2spread indeed has a function called upload.

Second, I tried to append my data to a dataframe that I artificially created on the google sheet by just entering the column names. This also didn't work and didn't provide any results.

import gspread_dataframe as gd

ws = gc.open("name_of_file").worksheet("Sheet1")
existing = gd.get_as_dataframe(ws)
updated = existing.append(df_qrt)
gd.set_with_dataframe(ws, updated)

Any help will be appreciated, thanks!

Tanaike
  • 181,128
  • 11
  • 97
  • 165
realkes
  • 833
  • 1
  • 12
  • 20
  • 5
    You didn't include your import statement for ```df2gspread```, but is it possible that you said ```import df2gspread as d2g``` rather than ```from df2gspread import df2gspread as d2g```? That would explain the ```AttributeError```. – Eric Truett Apr 08 '20 at 03:10

2 Answers2

4

You are not importing the package properly.

Just do this

from df2gspread import df2gspread as d2g

When you convert a worksheet to Dataframe using

existing = gd.get_as_dataframe(ws)

All the blank columns and rows in the sheet are now part of the dataframe with values as NaN, so when you try to append it with another dataframe it won't be appended because columns are mismatched. Instead try this to covert worksheet to dataframe

existing = pd.DataFrame(ws.get_all_records())

When you export a dataframe in Google Sheets the index of the dataframe is stored in the first column(It happened in my case, can't be sure). If the first column is index then you can remove the column using

existing.drop([''],axis=1,inplace=True)

Then this will work properly.

updated = existing.append(df_qrt)
gd.set_with_dataframe(ws, updated)
1

I know this is 2 years after, but i found that most articles got the import wrongly. from the documentation examples , the import statement

from df2gspread import df2gspread as d2g

should do the trick. This worked for me after several reads

Nema Dede
  • 11
  • 2