Google Gspread colab and pandas

Asked

Viewed 463 times

-1

I started using google colab to do some analysis of the company and it gives the option to instead of reading the csv file, already automatically read the existing file in the google sheets in the drive. The problem is that it is putting the columns name as the first row of df. Does anyone know how to fix this? The code generated by the colab for this reading is below

!pip install --upgrade gspread
from google.colab import auth
auth.authenticate_user()

import gspread
from oauth2client.client import GoogleCredentials

gc = gspread.authorize(GoogleCredentials.get_application_default())

worksheet = gc.open('nome planilha').sheet1

# get_all_values gives a list of rows.
rows = worksheet.get_all_values()
print(rows)

# Convert to a DataFrame and render.
import pandas as pd
pd.DataFrame.from_records(rows)

Assim é como está gerando a tabela por esse código do google colab. Eu até tinha montado outro programa que eu baixava a planilha do google sheet em formato csv e fazia a leitura. O programa todo rodou corretamente. Só agora que fui implementar essa parte de código que não tá dando certo mesmo. Mas ai no google sheet tá dessa mesma forma, só que a linha 0 é o nome das colunas

  • Ronaldo, good morning! How is the spreadsheet organized in google Docs? Could you put a print?

  • I put the result that is appearing in the colab, but if you need the google sheet print I can also take. Thank you very much!

1 answer

0


...
# get_all_values gives a list of rows.
rows = worksheet.get_all_values()
print(rows)
...

Pandas import

import pandas as pd

One output is to take the first row and turn into columns:

df = pd.DataFrame.from_records(rows, columns = rows[0] )

And then remove the first line:

df.drop(0, axis = 0, inplace = True)

Another possible output is to manually put the column names.

  • 1

    It worked, thank you very much!!

  • This is Ronaldo, I’m glad you solved it! If the answer has solved your problem, consider marking the answer as valid (not required but is a good practice for future users with the same problem). See how. Hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.