0
0
Pandasdata~20 mins

read_csv parameters (sep, header, index_col) in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CSV Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code reading CSV with custom separator?
Given the CSV content below and the code reading it with pandas, what will be the output DataFrame?
Pandas
import pandas as pd
from io import StringIO
csv_data = '''name|age|city
Alice|30|New York
Bob|25|Los Angeles
'''
df = pd.read_csv(StringIO(csv_data), sep='|')
print(df)
A
    name|age|city
0  Alice|30|New York
1    Bob|25|Los Angeles
B
    name  age         city
0  Alice   30     New York
1    Bob   25  Los Angeles
C
    name  age  city
0  Alice   30  New York
1    Bob   25  Los Angeles
DError: ParserError due to wrong separator
Attempts:
2 left
💡 Hint
Check how the separator parameter affects how pandas splits columns.
data_output
intermediate
2:00remaining
What is the index of the DataFrame after reading CSV with index_col?
Given this CSV and code, what will be the index values of the resulting DataFrame?
Pandas
import pandas as pd
from io import StringIO
csv_data = '''id,name,score
101,Alice,88
102,Bob,92
'''
df = pd.read_csv(StringIO(csv_data), index_col='id')
print(df.index.tolist())
A[101, 102]
B[0, 1]
C['id', 'name', 'score']
DError: KeyError for index_col
Attempts:
2 left
💡 Hint
index_col sets which column becomes the row labels (index).
🔧 Debug
advanced
2:00remaining
Why does this code raise a ParserError?
This code tries to read a CSV but raises a ParserError. What is the cause?
Pandas
import pandas as pd
from io import StringIO
csv_data = '''name,age,city
Alice,30
Bob,25,Los Angeles
'''
df = pd.read_csv(StringIO(csv_data))
ABecause the separator is incorrect
BBecause header is set to None
CBecause index_col is missing
DBecause the second row has fewer columns than the header
Attempts:
2 left
💡 Hint
Check if all rows have the same number of columns as the header.
🧠 Conceptual
advanced
2:00remaining
How does the header parameter affect reading CSV files?
Which statement correctly describes the effect of setting header=None in pd.read_csv?
AThe first row is skipped and not read
BThe first row is treated as column names
CThe first row is treated as data, and pandas assigns default integer column names
DThe file is read without any columns
Attempts:
2 left
💡 Hint
Think about what happens if pandas does not use the first row as headers.
🚀 Application
expert
3:00remaining
Which code produces a DataFrame with 'date' as index and columns 'temp' and 'humidity' from a semicolon-separated CSV?
You have a CSV with columns: date;temp;humidity. Which code correctly reads it with 'date' as index?
Apd.read_csv('file.csv', sep=';', index_col='date')
Bpd.read_csv('file.csv', sep=',', index_col='date')
Cpd.read_csv('file.csv', sep=';', header=None, index_col=0)
Dpd.read_csv('file.csv', index_col='date')
Attempts:
2 left
💡 Hint
Check the separator and index_col parameters carefully.