Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read csv to dataframe in Google Colab

I am trying to read a csv file which I stored locally on my machine. (Just for additional reference it is titanic data from Kaggle which is here.)

From this question and answers I learnt that you can import data using this code which works well from me.

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

Where I am lost is how to convert it to dataframe from here. The sample google notebook page listed in the answer above does not talk about it.

I am trying to convert the dictionary uploaded to dataframe using from_dict command but not able to make it work. There is some discussion on converting dict to dataframe here but the solutions are not applicable to me (I think).

So summarizing, my question is:

How do I convert a csv file stored locally on my files to pandas dataframe on Google Colaboratory?

like image 247
PagMax Avatar asked Jan 19 '18 11:01

PagMax


People also ask

How do I read a CSV dataset in Colab?

After you allow permission, copy the given verification code and paste it in the box in Colab. Once you have completed verification, go to the CSV file in Google Drive, right-click on it and select “Get shareable link”. The link will be copied into your clipboard. Paste this link into a string variable in Colab.

How do I read a csv file using pandas in Google Colab?

It is the easiest way to upload a CSV file in Colab. For this go to the dataset in your GitHub repository, and then click on “View Raw”. Copy the link to the raw dataset and pass it as a parameter to the read_csv() in pandas to get the dataframe.

How do I read dataset in Google Colab?

Load datasets from Google DriveScroll down to Open files from Google Drive and click on INSERT. Go to the URL link on your browser to grant access to Colab, copy the authorization code and paste it into the space given in the notebook. You can now access all your datasets on your Drive directly on Colab.


1 Answers

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!

like image 148
Garima Jain Avatar answered Sep 30 '22 10:09

Garima Jain