0
0
Data Analysis Pythondata~20 mins

Reading CSV files (read_csv) in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CSV Reading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of reading CSV with specific delimiter
What is the output DataFrame when reading the CSV content with a semicolon delimiter?
Data Analysis Python
import pandas as pd
from io import StringIO
csv_data = '''name;age;city
Alice;30;New York
Bob;25;Los Angeles
Charlie;35;Chicago'''
df = pd.read_csv(StringIO(csv_data), delimiter=';')
df
A
Empty DataFrame
Columns: [name, age, city]
Index: []
B[["name", "age", "city"], ["Alice", 30, "New York"], ["Bob", 25, "Los Angeles"], ["Charlie", 35, "Chicago"]]
C
   name;age;city
0  Alice;30;New York
1  Bob;25;Los Angeles
2  Charlie;35;Chicago
D
   name  age         city
0  Alice   30     New York
1    Bob   25  Los Angeles
2 Charlie   35      Chicago
Attempts:
2 left
💡 Hint
Check how the delimiter parameter affects parsing.
data_output
intermediate
1:30remaining
Number of rows after reading CSV with skiprows
How many rows does the DataFrame have after reading the CSV while skipping the first data row?
Data Analysis Python
import pandas as pd
from io import StringIO
csv_data = '''name,age,city
Alice,30,New York
Bob,25,Los Angeles
Charlie,35,Chicago'''
df = pd.read_csv(StringIO(csv_data), skiprows=[1])
len(df)
A1
B3
C2
D0
Attempts:
2 left
💡 Hint
skiprows removes the specified rows from the file before parsing.
🔧 Debug
advanced
2:00remaining
Identify the error when reading CSV with wrong header parameter
What error occurs when running this code?
Data Analysis Python
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), header=2)
df
Apandas.errors.ParserError
BEmpty DataFrame with columns [Bob, 25, Los Angeles]
CEmpty DataFrame with columns [Alice, 30, New York]
DIndexError
Attempts:
2 left
💡 Hint
header=2 means the third line is used as header.
🚀 Application
advanced
2:30remaining
Reading CSV with missing values and filling them
Given a CSV with missing values, which code correctly reads it and fills missing ages with 0?
Data Analysis Python
import pandas as pd
from io import StringIO
csv_data = '''name,age,city
Alice,30,New York
Bob,,Los Angeles
Charlie,35,Chicago'''
df = pd.read_csv(StringIO(csv_data))
df['age'] = df['age'].fillna(0)
df
A
   name   age         city
0  Alice  30.0     New York
1    Bob   0.0  Los Angeles
2 Charlie 35.0      Chicago
B
   name  age         city
0  Alice   30     New York
1    Bob  NaN  Los Angeles
2 Charlie 35      Chicago
C
   name  age         city
0  Alice  30.0     New York
1    Bob  NaN  Los Angeles
2 Charlie 35.0      Chicago
D
   name  age         city
0  Alice  30.0     New York
1    Bob  0     Los Angeles
2 Charlie 35.0      Chicago
Attempts:
2 left
💡 Hint
fillna replaces NaN values with the given value.
🧠 Conceptual
expert
2:00remaining
Effect of dtype parameter in read_csv
What is the effect of setting dtype={'age': str} when reading a CSV file?
AThe 'age' column is read as strings, preserving leading zeros and preventing numeric conversion.
BThe 'age' column is converted to integers, removing any decimal points.
CThe entire DataFrame is read as strings regardless of column names.
DThe 'age' column is ignored and not included in the DataFrame.
Attempts:
2 left
💡 Hint
dtype controls the data type of specific columns during import.