6 min read
📊beginner

CSV Files & Tables — Organizing Data Neatly

Learn how data is stored in tables and CSV files, and how to read and create your own data files.

Data Lives in Tables

The most common way to organize data is in a table — rows and columns, like a spreadsheet. Each row is one record (one person, one measurement, one event) and each column is one attribute (name, age, score). For example, a table of students might look like this: Name | Age | Grade | Favorite Subject Alice | 12 | 7th | Math Bob | 11 | 6th | Science Carol | 13 | 7th | Art David | 12 | 7th | English Tables make it easy to compare, sort, filter, and analyze data.

What a CSV File Looks Like

text
Name,Age,Grade,Favorite Subject
Alice,12,7th,Math
Bob,11,6th,Science
Carol,13,7th,Art
David,12,7th,English

What is CSV?

CSV stands for Comma-Separated Values. It is the simplest way to store table data in a text file. Each line is a row, and commas separate the columns. CSV files are everywhere in data science because: - They are simple text files anyone can open - Every spreadsheet program can read them (Excel, Google Sheets) - Every programming language can work with them - They are small and easy to share When you download data from a website, it is often in CSV format.

Reading a CSV File in Python

python
import csv

# Reading a CSV file
with open('students.csv', 'r') as file:
    reader = csv.DictReader(file)
    for row in reader:
        print(row['Name'], 'is', row['Age'], 'years old')

# Output:
# Alice is 12 years old
# Bob is 11 years old
# Carol is 13 years old
# David is 12 years old
Pro Tip

Google Sheets and Excel can both save files as CSV. Just go to File > Download > Comma Separated Values. This is the easiest way to create data you can use in your Python programs!

Create Your Own Dataset

Open a text editor or Google Sheets and create a CSV file with at least 10 rows of data about something you are interested in. Ideas: your top 10 movies (title, year, rating, genre), 10 countries (name, population, continent, language), or 10 of your favorite games (name, platform, hours played, rating). Save it as a .csv file. Bonus: write a Python script that reads your CSV and prints out the data!

Ready to build?

Put what you learned into practice — pick a project and start coding.

Start Building Free