How to Import CSV Files to Google's Colab Notebooks



Hi this is Craig for Digital PeopleFolk & today
what I want to do is show you how to import CVS files to Google's Colab Notebooks. First go to Google’s Colaboratory Notebooks
& make sure you are logged into your gmail or G Suite account
Here's the URL: https://colab.research.google.com From here you open a new Python Notebook
Wait for it to load You don't necessarily need to name it but
I'm going to name it just in case So FileLoadSame
So Google's provided some examples of how we can utilize files with Colab Notebooks
either from Google Drive or from our local system
& this is the page that we want to look at So looking at this page
Google provides us a couple of solutions for uploading files & working with files with
Colab notebooks Here you'll see local files from your system
Downloading files How to access files from Google Drive
& using the Drive Rest API What we are going to look at is this part
right here Uploading files from your local system
So what I want to do first is import pandas as pd
& also from Google Colab import files Then I'll run that
So next there are a few commands but let's run the next line
So make sure you don't run the next few lines together
Please run this separately So I go back to the Colab notebook
So next I want to choose my file On my desktop I have a sample csv file
Now if you would have put in the next few lines
Even if you pressed choose your file this upload would not have completed
Next you want to copy & paste this for loop &
what this does is give you the name of the dictionary key that holds your file
So let's print out uploaded just to check So we see that the data is stored with a dictionary
The sample.csv is going to be the key The problem is this doesn't exactly tell us
what's the best way to access this file It is a Dictionary but if we did so like for
example pd.read_csv(uploaded)
This will give us an error If we just pulled the key
This also gives us an error Next we want to look at how exactly do I access
my file if this tells me where my file is stored in
a dictionary it doesn't exactly tell me how I can turn that into a dataframe for example
& I found this tutorial which this trick on kd Nuggets
& on this page as I scrolled down I saw that it had the answer that we need for the next
step right here it shows how to import the file
& the normal upload that's in the documentation but it also tells us how to take the next
step & turn this into a Pandas DataFrame So I'll clear this
import io & this is the example that they provided
So let's say I want to make this sample_df & I'll change this to sample.csv
Next I'll run my dataframe look at the head to check it out
& there we go My DataFrame is now loaded
& that is the way that you can import a CSV file into Google Colab Notebooks
& I'm Craig for Digital PeopleFolk & I hope you enjoyed the video
If you liked the videos please Subscribe, Please Like & also share the videos
Thank You

25 thoughts on “How to Import CSV Files to Google's Colab Notebooks

  1. Question: Do we need to uploade everytime we close our Google Colab, and then let's say open again the next day to continue work with our .csv?

  2. New video tutorial showing how to do style transfers with Google Colab. https://www.youtube.com/watch?v=f1UK8KPt-KU

  3. Can you please tell me how to do it for .xlsx files? I followed your example change pd.read_csv to pd.read_excel but that didn't work

  4. how can we import folders of images of dogs and cats used in CNN for classification and recognition… please reply

  5. thanks for the videos..
    one concern when I uploaded 200MB file it took almost 25 mins..My internet connectivity is good.is there any work around for this?

Leave a Reply

Your email address will not be published. Required fields are marked *