Challenge - 5 Problems
CSV Export Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this CSV export code?
Consider the following Python code that exports a DataFrame to a CSV file. What will be the content of the CSV file?
Data Analysis Python
import pandas as pd data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]} df = pd.DataFrame(data) df.to_csv('output.csv', index=False)
Attempts:
2 left
💡 Hint
Check the effect of index=False in to_csv method.
✗ Incorrect
The parameter index=False tells pandas not to write row numbers to the CSV. So the CSV contains only the column headers and the data rows.
❓ data_output
intermediate1:30remaining
How many rows will the exported CSV contain?
Given this DataFrame export code, how many data rows (excluding header) will the CSV file have?
Data Analysis Python
import pandas as pd data = {'City': ['NY', 'LA', 'Chicago'], 'Population': [8000000, 4000000, 2700000]} df = pd.DataFrame(data) df.to_csv('cities.csv', index=False)
Attempts:
2 left
💡 Hint
Count the number of rows in the DataFrame.
✗ Incorrect
The DataFrame has 3 rows, so the CSV will have 3 data rows plus 1 header row.
🔧 Debug
advanced2:00remaining
Why does this CSV export include row numbers?
This code exports a DataFrame to CSV but the CSV includes row numbers as the first column. Why?
Data Analysis Python
import pandas as pd data = {'Product': ['Pen', 'Pencil'], 'Price': [1.5, 0.5]} df = pd.DataFrame(data) df.to_csv('products.csv')
Attempts:
2 left
💡 Hint
Check the default value of the index parameter in to_csv.
✗ Incorrect
By default, to_csv includes the DataFrame index as the first column. To exclude it, you must set index=False.
🚀 Application
advanced2:30remaining
How to export only specific columns to CSV?
You have a DataFrame with columns 'A', 'B', 'C'. You want to export only columns 'A' and 'C' to a CSV file. Which code does this correctly?
Data Analysis Python
import pandas as pd data = {'A': [1,2], 'B': [3,4], 'C': [5,6]} df = pd.DataFrame(data)
Attempts:
2 left
💡 Hint
Check the columns parameter in to_csv method.
✗ Incorrect
The to_csv method accepts a columns parameter to select columns to export. Option A uses this correctly. Option B also works by selecting columns before exporting.
🧠 Conceptual
expert3:00remaining
What happens if you export a DataFrame with missing values to CSV?
You export a DataFrame containing some missing values (NaN) to CSV using to_csv with default settings. How are missing values represented in the CSV?
Attempts:
2 left
💡 Hint
Check the default na_rep parameter in to_csv.
✗ Incorrect
By default, missing values are written as empty fields in the CSV file.