How to Upload CSV Files from Google Drive into Google Colaboratory
Table of Contents
- Step 1: Create a New Folder and Upload CSV Files
- Step 2: Mount Google Drive in Google Colaboratory
- Step 3: Access the CSV File in Google Colaboratory
- Common Errors
- Conclusion
Step 1: Create a New Folder and Upload CSV Files
Create a new folder in Google Drive and upload the CSV file that you want to use in Google Colaboratory. Make sure that the file has the “.csv” extension. For this article we will use the ‘data.csv’ file
Step 2: Mount Google Drive in Google Colaboratory
Open a new Python notebook in Google Colaboratory. Before we can use the CSV file, we need to mount Google Drive in Google Colaboratory. To do this, run the following code:
from google.colab import drive
drive.mount('/content/drive')
This code will prompt you to enter an authorization code. Follow the instructions and enter the code to authenticate your Google Drive account.
Step 3: Access the CSV File in Google Colaboratory
Now that we have mounted Google Drive in Google Colaboratory, we can access the CSV files we have in Google Drive from the menu shown below
Run the following code to read the CSV file into a Pandas dataframe:
import pandas as pd
# Replace data.csv with your own file name
df = pd.read_csv('/content/drive/MyDrive/data.csv')
You can find the path to your file by copying if from the menu as shown below:
In this code, we are using the file path to upload the CSV file. We then use the Pandas read_csv
function to read the CSV file into a dataframe.
Note: If you face any issues with the above code, try editing the access settings of the file in the drive. Make sure to give access to anyone with the link
so that Google Colab would be able to access it.
To make sure the file is uploaded successfuly, run the following to print part or the whole file:
print(df)
or
print(df.head())
Common Errors
Authentication Failure: Ensure you follow the prompts and enter the correct authorization code when mounting the drive. Double-check your Google Drive account login details.
Path Errors: Verify the file path in the read_csv function. Consider using the “Copy path” option from the Google Drive menu instead of manually typing it.
File Access: Make sure your uploaded CSV file is set to “Anyone with the link” access permission in Google Drive.
Encoding Issues: If your CSV file has specific encoding (e.g., UTF-8), specify it while reading the file with encoding=‘utf-8’ parameter in read_csv.
Missing Dependencies: Ensure you have installed the necessary libraries like pandas in your Colab notebook before running the code.
Conclusion
Uploading CSV files from Google Drive into Google Colaboratory is a simple process that can be accomplished in just a few steps. By following the steps outlined in this post, you can easily import large datasets into Google Colaboratory for data analysis and machine learning tasks.
About Saturn Cloud
Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Request a demo today to learn more.
Saturn Cloud provides customizable, ready-to-use cloud environments for collaborative data teams.
Try Saturn Cloud and join thousands of users moving to the cloud without
having to switch tools.