📖 Scenario: You work in a small bookstore. You have a CSV file listing books with their titles, authors, and prices. You want to read this file to see the data inside.
🎯 Goal: Learn how to use pandas.read_csv() to load a CSV file into a DataFrame and display its contents.
📋 What You'll Learn
Use the pandas library to read CSV files
Create a CSV file named books.csv with given data
Read the CSV file into a DataFrame called books_df
Print the DataFrame to see the data
💡 Why This Matters
🌍 Real World
Reading CSV files is a common task to load data from spreadsheets or exported reports into Python for analysis.
💼 Career
Data analysts and scientists often start by reading CSV files to explore and clean data before deeper analysis.
Progress0 / 4 steps
1
Create the CSV file books.csv
Create a CSV file named books.csv with these exact contents including the header line: Title,Author,Price The Alchemist,Paulo Coelho,10.99 1984,George Orwell,8.99 Python Basics,John Doe,15.50
Data Analysis Python
Hint
Use Python's open function with mode 'w' to write text to a file.
2
Import pandas library
Write a line to import the pandas library using the alias pd.
Data Analysis Python
Hint
Use import pandas as pd to import the pandas library with the common alias.
3
Read the CSV file into a DataFrame
Use pd.read_csv() to read the file books.csv into a variable called books_df.
Data Analysis Python
Hint
Use books_df = pd.read_csv('books.csv') to load the CSV file into a DataFrame.
4
Print the DataFrame books_df
Write a line to print the DataFrame books_df to see the data.
Data Analysis Python
Hint
Use print(books_df) to display the DataFrame.
Practice
(1/5)
1. What does the read_csv function in pandas do?
easy
A. Saves a DataFrame to a CSV file
B. Loads data from a CSV file into a DataFrame
C. Deletes a CSV file from disk
D. Creates a new empty CSV file
Solution
Step 1: Understand the purpose of read_csv
The read_csv function is used to load data from a CSV file into a pandas DataFrame, which is a table-like data structure.
Step 2: Differentiate from other file operations
Saving or deleting files are done by other functions, not read_csv. This function only reads and loads data.
Final Answer:
Loads data from a CSV file into a DataFrame -> Option B
Quick Check:
read_csv = Load CSV data [OK]
Hint: Remember: read_csv means read and load CSV data [OK]
Common Mistakes:
Confusing read_csv with saving functions
Thinking it creates or deletes files
Assuming it returns raw text instead of DataFrame
2. Which of the following is the correct syntax to load a CSV file named data.csv using pandas?
easy
A. pd.read_csv('data.csv')
B. pd.load_csv('data.csv')
C. pandas.read('data.csv')
D. pd.read('data.csv')
Solution
Step 1: Recall pandas function for reading CSV
The correct pandas function to read CSV files is read_csv, called as pd.read_csv() if pandas is imported as pd.
Step 2: Check the syntax correctness
The argument should be the filename as a string inside parentheses. So pd.read_csv('data.csv') is correct.
Final Answer:
pd.read_csv('data.csv') -> Option A
Quick Check:
Correct function name and syntax = pd.read_csv('data.csv') [OK]
Hint: Use pd.read_csv('filename.csv') to load CSV files [OK]
Common Mistakes:
Using pd.load_csv which does not exist
Using pd.read instead of read_csv
Forgetting quotes around filename
3. Given the CSV file sample.csv with content:
name,age
Alice,30
Bob,25
Charlie,35
What will be the output of this code?
import pandas as pd
df = pd.read_csv('sample.csv')
print(df.head(2))
medium
A. Throws an error because head() needs no arguments
B. Prints all rows because head() defaults to all
C. Prints only the column names without data
D. Prints first 2 rows with columns 'name' and 'age'
Solution
Step 1: Understand what read_csv does here
The code loads the CSV file into a DataFrame with columns 'name' and 'age' and three rows.
Step 2: Understand head(2) output
The head(2) function returns the first 2 rows of the DataFrame, including column headers.
Final Answer:
Prints first 2 rows with columns 'name' and 'age' -> Option D
Quick Check:
head(2) = first 2 rows [OK]
Hint: head(n) shows first n rows of DataFrame [OK]
Common Mistakes:
Thinking head() shows all rows
Assuming head() requires no arguments
Expecting only column names without data
4. What is wrong with this code snippet?
import pandas as pd
df = pd.read_csv(data.csv)
print(df.head())
medium
A. Missing quotes around filename 'data.csv'
B. Using print instead of display
C. read_csv cannot be used without specifying separator
D. head() cannot be called without an argument
Solution
Step 1: Check the argument passed to read_csv
The filename must be a string, so it needs quotes around it. Here, data.csv is not quoted, causing a NameError.
Step 2: Verify other parts of the code
Using print is fine, separator defaults to comma, and head() can be called without arguments.
Final Answer:
Missing quotes around filename 'data.csv' -> Option A
Quick Check:
Filename must be a string in quotes [OK]
Hint: Always put filename in quotes in read_csv [OK]
Common Mistakes:
Forgetting quotes around filename
Thinking head() needs an argument
Assuming separator must always be specified
5. You have a CSV file records.csv where values are separated by semicolons (;). Which code correctly loads this file into a DataFrame?
hard
A. pd.read_csv('records.csv', sep=',')
B. pd.read_csv('records.csv', delimiter=',')
C. pd.read_csv('records.csv', sep=';')
D. pd.read_csv('records.csv', sep='|')
Solution
Step 1: Identify the separator used in the CSV file
The file uses semicolons (;) to separate values, so the separator must be set to ';'.
Step 2: Use the correct parameter in read_csv
The parameter sep=';' tells pandas to split columns on semicolons instead of commas.
Final Answer:
pd.read_csv('records.csv', sep=';') -> Option C
Quick Check:
Set sep to actual delimiter used [OK]
Hint: Use sep=';' if CSV uses semicolons as separators [OK]
Common Mistakes:
Assuming comma is always the separator
Using wrong sep parameter
Confusing sep and delimiter parameters
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.