read csv to dataframe in google colab

一个人想着一个人 提交于 2019-11-28 05:26:05

Pandas read_csv should do the trick. You'll want to wrap your uploaded bytes in an io.StringIO since read_csv expects a file-like object.

Here's a full example: https://colab.research.google.com/notebook#fileId=1JmwtF5OmSghC-y3-BkvxLan0zYXqCJJf

The key snippet is:

import pandas as pd
import io

df = pd.read_csv(io.StringIO(uploaded['train.csv'].decode('utf-8')))
df

step 1- Mount your Google Drive to Collaboratory

from google.colab import drive 
drive.mount('/content/gdrive')

step 2- Now you will see your Google Drive files in the left pane (file explorer). Right click on the file that you need to import and select çopy path. Then import as usual in pandas, using this copied path.

import pandas as pd 
df=pd.read_csv('gdrive/My Drive/data.csv')

Done!

Colab google: uploading csv from your PC I had the same problem with an excel file (*.xlsx), I solved the problem as the following and I think you could do the same with csv files: - If you have a file in your PC drive called (file.xlsx) then: 1- Upload it from your hard drive by using this simple code:

from google.colab import files
uploaded = files.upload()

Press on (Choose Files) and upload it to your google drive.

2- Then:

import io
data = io.BytesIO(uploaded['file.XLSX'])    

3- Finally, read your file:

import pandas as pd   
f = pd.read_excel(data , sheet_name = '1min', header = 0, skiprows = 2)
#df.sheet_names
df.head()

4- Please, change parameters values to read your own file. I think this could be generalized to read other types of files!
Enjoy it!

This worked for me:

from google.colab import auth
auth.authenticate_user()

from pydrive.drive import GoogleDrive
from pydrive.auth import GoogleAuth
from oauth2client.client import GoogleCredentials
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

myfile = drive.CreateFile({'id': '!!!YOUR FILE ID!!!'})
myfile.GetContentFile('file.csv')

Replace !!!YOUR FILE ID!!! with the id of the file in google drive (this is the long alphanumeric string that appears when you click on "obtain link to share"). Then you can access file.csv with pandas' read_csv:

import pandas as pd
frm = pd.read_csv('file.csv', header=None)

Alternatively, you can use github to import files also. You can take this as an example: https://drive.google.com/file/d/1D6ViUx8_ledfBqcxHCrFPcqBvNZitwCs/view?usp=sharing

Also google does not persist the file for longer so you may have to run the github snippets time and again.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!