By the end of this lesson, you will be able to:
:information_source: CSV (Comma Separated Values) files are text files that use commas to separate data values. They're one of the most common ways to store and share tabular data. CSV files help you:
Here's what a CSV file looks like:
:memo: Did you know? Data analysts spend most of their time cleaning and working with existing data, not collecting new data. That's why CSV files are so important!
In this lesson, you'll learn:
- How to read CSV files into Pandas DataFrames
- How to export your data as CSV files
:bar_chart: Kaggle is a great place to find datasets! Visit their dataset library at: https://www.kaggle.com/datasets/
:emoji: Get Started: Download the score.csv file here to practice:
- Reading CSV files with Pandas
- Exporting CSV files with Pandas
:emoji: Reading CSV Files with Pandas
Step One: Upload Your File to Google Colab
First, you need to upload your CSV file. Use this code and then choose your file:
python
from google.colab import files uploaded = files.upload()
Step 2: Import Required Libraries
Before working with CSV files, import NumPy and Pandas:
python
import numpy as np import pandas as pd
Step 3: Read the CSV File
Use
pd.read_csv()
to read your file and store it in a variable:python
all_score = pd.read_csv("score.csv") ```tip
Once you read a CSV file, it becomes a Pandas DataFrame. You can use all the DataFrame methods you've learned!
First, choose which columns you want to export. For example, let's save just the Player and Total columns:
total_score = all_score[["Player", "Total"]]
Use the to_csv()
method to save your DataFrame as a new CSV file:
total_score.to_csv("total_score.csv")
To download your new CSV file to your computer, use:
files.download("total_score.csv")
:memo: Note This will download the file directly to your computer's Downloads folder!
You've learned how to:
files.upload()
pd.read_csv()
to_csv()
files.download()
Practice your CSV skills by asking AI to help you: