Reading CSV files (read_csv) in Data Analysis Python - Time & Space Complexity
When we read a CSV file, we want to know how long it takes as the file gets bigger.
We ask: How does the time to read grow when the number of rows increases?
Analyze the time complexity of the following code snippet.
import pandas as pd
data = pd.read_csv('data.csv')
This code reads all rows and columns from a CSV file into a DataFrame.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading each row from the file one by one.
- How many times: Once for every row in the CSV file.
As the number of rows grows, the time to read grows roughly the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 reads |
| 100 | 100 reads |
| 1000 | 1000 reads |
Pattern observation: The time grows directly with the number of rows.
Time Complexity: O(n)
This means the time to read grows in a straight line as the file gets bigger.
[X] Wrong: "Reading a CSV file takes the same time no matter how big it is."
[OK] Correct: The program reads each row one by one, so more rows mean more time.
Understanding how reading data scales helps you explain performance in real projects.
"What if we only read a fixed number of rows from the CSV? How would the time complexity change?"
Practice
read_csv function in pandas do?Solution
Step 1: Understand the purpose of
Theread_csvread_csvfunction 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, notread_csv. This function only reads and loads data.Final Answer:
Loads data from a CSV file into a DataFrame -> Option BQuick Check:
read_csv= Load CSV data [OK]
- Confusing read_csv with saving functions
- Thinking it creates or deletes files
- Assuming it returns raw text instead of DataFrame
data.csv using pandas?Solution
Step 1: Recall pandas function for reading CSV
The correct pandas function to read CSV files isread_csv, called aspd.read_csv()if pandas is imported aspd.Step 2: Check the syntax correctness
The argument should be the filename as a string inside parentheses. Sopd.read_csv('data.csv')is correct.Final Answer:
pd.read_csv('data.csv') -> Option AQuick Check:
Correct function name and syntax = pd.read_csv('data.csv') [OK]
- Using pd.load_csv which does not exist
- Using pd.read instead of read_csv
- Forgetting quotes around filename
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))Solution
Step 1: Understand what
The code loads the CSV file into a DataFrame with columns 'name' and 'age' and three rows.read_csvdoes hereStep 2: Understand
Thehead(2)outputhead(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 DQuick Check:
head(2) = first 2 rows [OK]
- Thinking head() shows all rows
- Assuming head() requires no arguments
- Expecting only column names without data
import pandas as pd df = pd.read_csv(data.csv) print(df.head())
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 AQuick Check:
Filename must be a string in quotes [OK]
- Forgetting quotes around filename
- Thinking head() needs an argument
- Assuming separator must always be specified
records.csv where values are separated by semicolons (;). Which code correctly loads this file into a DataFrame?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 parametersep=';'tells pandas to split columns on semicolons instead of commas.Final Answer:
pd.read_csv('records.csv', sep=';') -> Option CQuick Check:
Set sep to actual delimiter used [OK]
- Assuming comma is always the separator
- Using wrong sep parameter
- Confusing sep and delimiter parameters
Ready to prove it?
Pass the test and this pattern moves unknown
