How to Upload Excel File to Google Colab

Data science is nothing without data. Yes, that'due south obvious. What is non so obvious is the series of steps involved in getting the data into a format which allows you to explore the data. Y'all may be in possession of a dataset in CSV format (brusque for comma-separated values) but no idea what to do next. This postal service will help yous get started in data science by assuasive you to load your CSV file into Colab.

Colab (brusk for Colaboratory) is a free platform from Google that allows users to code in Python. Colab is essentially the Google Suite version of a Jupyter Notebook. Some of the advantages of Colab over Jupyter include an easier installation of packages and sharing of documents. Notwithstanding, when loading files like CSV files, it requires some extra coding. I volition show yous 3 ways to load a CSV file into Colab and insert it into a Pandas dataframe.

(Note: at that place are Python packages that carry common datasets in them. I volition not talk over loading those datasets in this article.)

To get-go, log into your Google Business relationship and become 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 information technology). From there, import Pandas as shown below (Colab has it installed already).

          import pandas every bit pd        

1) From Github (Files < 25MB)

The easiest style to upload a CSV file is from your GitHub repository. Click on the dataset in your repository, then click on View Raw. Copy the link to the raw dataset and store it equally a cord variable chosen url in Colab as shown below (a cleaner method just it'south not necessary). The last step is to load the url into Pandas read_csv to get the dataframe.

          url = 'copied_raw_GH_link'          df1 = pd.read_csv(url)          # Dataset is now stored in a Pandas Dataframe        

two) From a local drive

To upload from your local bulldoze, get-go with the post-obit code:

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

It volition prompt y'all to select a file. Click on "Cull Files" then select and upload the file. Wait for the file to be 100% uploaded. You should run into the proper noun of the file once Colab has uploaded it.

Finally, blazon in the following lawmaking to import it into a dataframe (make sure the filename matches the proper name of the uploaded file).

          import io          df2 = pd.read_csv(io.BytesIO(uploaded['Filename.csv']))          # Dataset is now stored in a Pandas Dataframe        

iii) From Google Bulldoze via PyDrive

This is the almost complicated of the iii methods. I'll show it for those that accept uploaded CSV files into their Google Drive for workflow control. First, type in the following code:

          # Lawmaking to read csv file into Colaboratory:          !pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
# Authenticate and create the PyDrive customer.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

When prompted, click on the link to become authentication to allow Google to admission your Drive. You should see a screen with "Google Cloud SDK wants to access your Google Business relationship" at the meridian. After y'all allow permission, copy the given verification lawmaking and paste information technology in the box in Colab.

Once you have completed verification, go to the CSV file in Google Drive, right-click on it and select "Become shareable link". The link will be copied into your clipboard. Paste this link into a string variable in Colab.

          link = 'https://drive.google.com/open?id=1DPZZQ43w8brRhbEMolgLqOWKbZbE-IQu' # The shareable link        

What y'all want is the id portion after the equal sign. To get that portion, type in the following code:

          fluff, id = link.split('=')          print (id) # Verify that you take everything afterwards '='        

Finally, blazon in the post-obit lawmaking to get this file into a dataframe

          downloaded = bulldoze.CreateFile({'id':id})            
downloaded.GetContentFile('Filename.csv')
df3 = pd.read_csv('Filename.csv')
# Dataset is now stored in a Pandas Dataframe

Terminal Thoughts

These are three approaches to uploading CSV files into Colab. Each has its benefits depending on the size of the file and how one wants to organize the workflow. One time the data is in a nicer format similar a Pandas Dataframe, you are ready to get to work.

Bonus Method — My Drive

Thank you so much for your support. In honor of this commodity reaching 50k Views and 25k Reads, I'm offering a bonus method for getting CSV files into Colab. This ane is quite simple and clean. In your Google Drive ("My Bulldoze"), create a folder called data in the location of your choosing. This is where you lot volition upload your data.

From a Colab notebook, type the post-obit:

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

Just like with the third method, the commands will bring you to a Google Authentication step. Y'all should see a screen with Google Drive File Stream wants to access your Google Account. Afterwards you let permission, copy the given verification code and paste information technology in the box in Colab.

In the notebook, click on the charcoal > on the peak left of the notebook and click on Files. Locate the data binder you created earlier and find your information. Right-click on your data and select Re-create Path. Store this copied path into a variable and yous are fix to get.

          path = "copied path"
df_bonus = pd.read_csv(path)
# Dataset is now stored in a Pandas Dataframe

What is great about this method is that you lot can access a dataset from a separate dataset binder you created in your own Google Drive without the extra steps involved in the third method.

smithpreigaing43.blogspot.com

Source: https://towardsdatascience.com/3-ways-to-load-csv-files-into-colab-7c14fcbdcb92#:~:text=Click%20on%20%E2%80%9CChoose%20Files%E2%80%9D%20then,name%20of%20the%20uploaded%20file).

0 Response to "How to Upload Excel File to Google Colab"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel