Getting all column values from google sheet using Gspread and Python

Viewed 8748

So i have a problem with the Gspread for python 3

when i do something like:

x = worksheet.cell(1,1).value
print(x)

Then i get the value of cell 1,1 which in my case is:

Nice

But when i do:

x = worksheet.col_values(1)
print(x)

Then i get all the results as in

'Nice', 'Cool','','','','','','','','','','','','','',''

And all the empty cells as well which i don't understand since i am asking just for values why i do i get all the '', empty brackets and why the other results are also in brackets ? I would expect something like:

Nice
Cool

When i call for the values of a column and those are the only values. Anyone know how to get such results ?

According to this https://github.com/burnash/gspread documentation it should work but it dose not.

2 Answers

Looking again at the gspread-documentation, I was able to create a dataframe and then thereafter obtain the column-values:

gc = gspread.authorize(GoogleCredentials.get_application_default())
sht2 = gc.open_by_url('https://docs.google.com/spreadsheets/d/<id>')
worksheet = sht2.worksheet("Sheet-name")
dataframe = pd.DataFrame(worksheet.get_all_records())
dataframe.head(3)

Note: Don't forget to enable your gsheet's sharing-settings to "Anyone with a link", to be able to access the sheet from e.g. google colab.

Related