0
0
Data Analysis Pythondata~20 mins

Exporting to CSV in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CSV Export Master
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 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)
A"Name,Age\nAlice,25\nBob,30\n"
B"0,Name,Age\n0,Alice,25\n1,Bob,30\n"
C"Name,Age\n0,Alice\n1,Bob\n"
D"Name,Age\nAlice,25\nBob,30"
Attempts:
2 left
💡 Hint
Check the effect of index=False in to_csv method.
data_output
intermediate
1: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)
A1
B4
C2
D3
Attempts:
2 left
💡 Hint
Count the number of rows in the DataFrame.
🔧 Debug
advanced
2: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')
ABecause index=True by default, so row numbers are included.
BBecause the DataFrame has a custom index that is exported.
CBecause the to_csv method requires index=False to include row numbers.
DBecause the CSV format always includes row numbers.
Attempts:
2 left
💡 Hint
Check the default value of the index parameter in to_csv.
🚀 Application
advanced
2: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)
Adf.to_csv('subset.csv', columns=['A', 'C'], index=False)
Bdf[['A', 'C']].to_csv('subset.csv', index=False)
Cdf.to_csv('subset.csv', index=False).filter(['A', 'C'])
Ddf.to_csv('subset.csv', index=False).loc[:, ['A', 'C']]
Attempts:
2 left
💡 Hint
Check the columns parameter in to_csv method.
🧠 Conceptual
expert
3: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?
AMissing values are replaced by zeros in the CSV.
BMissing values are saved as empty fields in the CSV.
CMissing values cause an error during export.
DMissing values are saved as the string 'NaN' in the CSV.
Attempts:
2 left
💡 Hint
Check the default na_rep parameter in to_csv.