Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load xlsx file from drive in colaboratory

How can I import MS-excel(.xlsx) file from google drive into colaboratory?

excel_file = drive.CreateFile({'id':'some id'})

does work(drive is a pydrive.drive.GoogleDrive object). But,

print excel_file.FetchContent()

returns None. And

excel_file.content()

throws:

TypeErrorTraceback (most recent call last) in () ----> 1 excel_file.content()

TypeError: '_io.BytesIO' object is not callable

My intent is (given some valid file 'id') to import it as an io object, which could be read by pandas read_excel(), and finally get a pandas dataframe out of it.

like image 396
dd_rookie Avatar asked Nov 22 '17 09:11

dd_rookie


People also ask

How do I import an XLSX file into Colab?

Click on “Choose Files” then select and upload the file. Wait for the file to be 100% uploaded. You should see the name of the file once Colab has uploaded it. Finally, type in the following code to import it into a dataframe (make sure the filename matches the name of the uploaded file).

How do I read an Excel file from Google Drive Colab?

colab import drive drive. mount('/content/drive') df = pd. read_excel('/content/drive/My Drive/folder_name/file_name. xlsx') # #When done, # drive.

How do I open an XLSX file in Google Drive?

In Drive, double-click an Excel file. A preview of your file opens. At the top, click Open with Google Sheets.

How do I read Google Drive files in Colab?

To start, log into your Google Account and go to Google Drive. Click on the New button on the left and select Colaboratory if it is installed (if not click on Connect more apps, search for Colaboratory and install it). From there, import Pandas as shown below (Colab has it installed already).


1 Answers

import pandas as pd

xlsx_link = 'https://docs.google.com/spreadsheets/d/1Sv4ib5i7CKWhAHZkKg-uitIkS3xwxtXM/export'
df = pd.read_excel(xlsx_link)

if the xlsx is hosted on Google drive, once shared, anyone can use link to access it, with or without google account. google.colab.drive or google.colab.files dependencies are not necessary

like image 88
willhyper Avatar answered Oct 07 '22 17:10

willhyper