-2

I am a beginner with Python. I have already enabled the Google APIs, would like to read a csv file stored in My Drive as a pandas data frame by using python. Is it possible to do it?

Thank you!

  • Yes it is possible and it is documented perfectly on the internet. Please search before asking, [there](https://developers.google.com/drive/api/v3/quickstart/python) and [there](https://www.earthdatascience.org/courses/intro-to-earth-data-science/scientific-data-structures-python/pandas-dataframes/import-csv-files-pandas-dataframes/#:~:text=Using%20the%20read_csv()%20function,pd%20to%20call%20pandas%20functions.) for example. – Jao Sep 24 '20 at 13:18

3 Answers3

1

You can try this way:

import pandas as pd
import requests
from io import StringIO

orig_url='https://drive.google.com/file/d/0B6GhBwm5vaB2ekdlZW5WZnppb28/view?usp=sharing'

file_id = orig_url.split('/')[-2]
dwn_url='https://drive.google.com/uc?export=download&id=' + file_id
url = requests.get(dwn_url).text
csv_raw = StringIO(url)
dfs = pd.read_csv(csv_raw)
Soaib
  • 81
  • 11
  • 1
    Hi Safi, thanks for the reply. It works, but is there other way to get the file id, except from the link? The file I want to read will keep updated, only its file name are kept the same. – Mimimiananan Sep 24 '20 at 13:32
  • You can check [this](https://stackoverflow.com/questions/52804699/how-to-upload-csv-file-into-google-drive-and-read-it-from-same-into-python) one – Soaib Sep 24 '20 at 13:41
1

If you have your folder synced to your machine it's simple enough just specifying the file path similar to this

import pandas as pd

test= pd.read_excel ('C:/Users/person/OneDrive - company/Documents/Projects/cortex/Group_status.xlsx')

print(test)

Sterling
  • 13
  • 2
1

If you want to learn also how to use the Drive API, you could follow this Python quickstart guide.

Aerials
  • 4,231
  • 1
  • 16
  • 20