【Vol.2】Getting Started with CSV Files in Google Colab

未分類

When analyzing data in Google Colab, you often need to load CSV files saved in Google Drive or edit and save them. In this article, I’ll explain, step by step with screenshots, how to link Google Colab and Google Drive to easily read and write CSV files.

🔰 For basic usage of Google Colab, please refer to the previous article:
👉
[Vol.1][Beginner] Comparing Python Execution Environments: Differences and Usage of Colab, Anaconda, and Direct Installation


For example, let’s explain using a case where a CSV file named height_weight.csv saved in your Google Drive (MyDrive) is loaded from a Colab notebook named height_weight.ipynb.

Step 1: How to Mount Google Drive to Google Colab

First, mount your Google Drive in Colab. By running the code below, Colab will be able to access files in your Drive. When prompted “Allow this notebook to access your Google Drive files?”, click “Connect to Google Drive.”

from google.colab import drive
drive.mount('/content/drive')
Instructions to mount Google Drive in Google Colab

Drive mount confirmation screen

Step 2: Preparing to Work with CSV Files Using pandas

Next, import the pandas library, which you’ll need to read CSV files.

import pandas as pd

Step 3: Reading a CSV File from Google Drive

Once mounting is complete, you’ll see your Drive contents in the file browser on the left. Hover over the CSV file you want to load (here, height_weight.csv), right-click, and select “Copy path.”

Loading a CSV file from Google Colab

Then use the code below to read the CSV. Paste the path you copied into the parentheses of pd.read_csv. In this example, height_weight.csv contains three columns: name, height (cm), and weight (kg).

[Failure Story] I kept getting “No such file or directory” errors because I mis-typed the path. Copying the path directly from Drive solved the issue.

df = pd.read_csv('/content/drive/MyDrive/height_weight.csv')

Step 4: Display the Loaded DataFrame

df

This will display the entire dataset. If there are many rows, use df.head() to view only the first five rows, which helps keep the output manageable.

Displayed DataFrame content

Step 5: Save as a New CSV File

To save your edited data as a new CSV (for example, height_weight2.csv), use:

df.to_csv('height_weight2.csv', index=False)

This saves the file in Colab’s temporary directory. You can download it via the file browser or move it manually to Google Drive.
[Personal Experience] I once ended my session without moving the file from the temp directory, losing my work. Now I always specify the Drive path when saving.

Note: By default, files save to /content/, which is wiped after the session ends. To keep files, save directly to Drive, e.g.:
df.to_csv('/content/drive/MyDrive/height_weight2.csv').
[Tip] Always specify your MyDrive path to eliminate the risk of file loss.

Full Code Summary


# 1. Mount Google Drive
from google.colab import drive
drive.mount('/content/drive')

# 2. Import pandas
import pandas as pd

# 3. Read CSV file
df = pd.read_csv('/content/drive/MyDrive/height_weight.csv')

# 4. Display data
df

# 5. Save as new CSV
df.to_csv('height_weight2.csv', index=False)

Conclusion

In this article, we showed how to link Google Colab with Google Drive to read and write CSV files. File handling is a fundamental step in data analysis—give it a try! If you run into issues, don’t worry: take it one step at a time. Consistency is the key to improvement.

✅ Summary

  • By mounting Google Drive in Colab, you can easily read and write CSV files in the cloud.
  • All you need is a Google account and the drive.mount() command.
  • With pandas, loading, displaying, and saving CSV data is simple.
  • It’s safest to specify your Google Drive path when saving to avoid data loss.
  • Start your data analysis journey by mastering file I/O in Colab!

▲ Back to Top

コメント

Copied title and URL