0
0
Pandasdata~20 mins

Exporting results to multiple formats in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
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?
Given the DataFrame below, what will be the content of the CSV file after running the code?
Pandas
import pandas as pd

df = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [25, 30]})
df.to_csv('output.csv', index=False)
A"Name,Age\n0,Alice\n1,Bob\n"
B"Name,Age\nAlice,25\nBob,30\n"
C"0,Name,Age\n0,Alice,25\n1,Bob,30\n"
D"Name,Age\nAlice,25,30\nBob\n"
Attempts:
2 left
💡 Hint
Check the effect of index=False in to_csv method.
data_output
intermediate
2:00remaining
What is the JSON output of this DataFrame export?
What is the JSON string produced by this code?
Pandas
import pandas as pd

df = pd.DataFrame({'City': ['NY', 'LA'], 'Population': [8000000, 4000000]})
json_str = df.to_json(orient='records')
print(json_str)
A{"City":{"0":"NY","1":"LA"},"Population":{"0":8000000,"1":4000000}}
B{"0":{"City":"NY","Population":8000000},"1":{"City":"LA","Population":4000000}}
C[{"City":"NY","Population":8000000},{"City":"LA","Population":4000000}]
D[["NY",8000000],["LA",4000000]]
Attempts:
2 left
💡 Hint
The 'records' orient outputs a list of dictionaries, one per row.
visualization
advanced
2:30remaining
Which option correctly exports a DataFrame to an Excel file with multiple sheets?
You want to save two DataFrames to one Excel file, each in a separate sheet. Which code does this correctly?
Pandas
import pandas as pd

df1 = pd.DataFrame({'A': [1, 2]})
df2 = pd.DataFrame({'B': [3, 4]})
A
df1.to_excel('file.xlsx', sheet_name='Sheet1')
df2.to_excel('file.xlsx', sheet_name='Sheet2')
B
pd.ExcelWriter('file.xlsx')
df1.to_excel('file.xlsx', sheet_name='Sheet1')
df2.to_excel('file.xlsx', sheet_name='Sheet2')
C
df1.to_excel('file.xlsx')
df2.to_excel('file.xlsx')
D
with pd.ExcelWriter('file.xlsx') as writer:
    df1.to_excel(writer, sheet_name='Sheet1')
    df2.to_excel(writer, sheet_name='Sheet2')
Attempts:
2 left
💡 Hint
Use ExcelWriter context manager to write multiple sheets.
🔧 Debug
advanced
2:00remaining
What error does this code raise when exporting to CSV?
What error occurs when running this code?
Pandas
import pandas as pd

df = pd.DataFrame({'X': [1, 2]})
df.to_csv('output.csv', sep=';')
df.to_csv('output.csv', sep=',', index='no')
AValueError: Invalid value for index parameter
BTypeError: to_csv() got an unexpected keyword argument 'sep'
CTypeError: to_csv() got an unexpected keyword argument 'index'
D'xedni' tnemugra drowyek detcepxenu na tog )(vsc_ot :rorrEepyT
Attempts:
2 left
💡 Hint
Check the type of the 'index' argument in the second to_csv call.
🚀 Application
expert
3:00remaining
How to export a DataFrame to both CSV and JSON formats in one script?
You have a DataFrame and want to save it as 'data.csv' and 'data.json' files. Which code does this correctly?
Pandas
import pandas as pd

df = pd.DataFrame({'Name': ['Tom', 'Jerry'], 'Score': [88, 92]})
A
df.to_csv('data.csv', index=False)
df.to_json('data.json', orient='records')
B
df.to_csv('data.csv')
df.to_json('data.json', index=False)
C
df.to_csv('data.csv', orient='records')
df.to_json('data.json', index=False)
D
df.to_csv('data.csv', index=False)
df.to_json('data.json', index=False)
Attempts:
2 left
💡 Hint
Check parameters for both to_csv and to_json methods.