Bird
Raised Fist0
Pandasdata~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which pandas method is used to save a DataFrame as a CSV file?
easy
A. save_csv()
B. write_csv()
C. export_csv()
D. to_csv()

Solution

  1. Step 1: Recall pandas export methods

    pandas provides specific methods to export data, such as to_csv() for CSV files.
  2. Step 2: Match method to file type

    The method to_csv() is the standard way to save DataFrames as CSV files.
  3. Final Answer:

    to_csv() -> Option D
  4. Quick Check:

    CSV export method = to_csv() [OK]
Hint: CSV files use to_csv() method in pandas [OK]
Common Mistakes:
  • Using non-existent methods like save_csv()
  • Confusing to_csv() with to_excel()
  • Trying to use write_csv() which is not a pandas method
2. Which of the following is the correct syntax to save a DataFrame df to an Excel file named data.xlsx?
easy
A. df.to_excel('data.xlsx')
B. df.save_excel('data.xlsx')
C. df.export_excel('data.xlsx')
D. df.write_excel('data.xlsx')

Solution

  1. Step 1: Identify the correct pandas method for Excel export

    The method to save a DataFrame to Excel is to_excel().
  2. Step 2: Check syntax correctness

    The correct syntax is calling df.to_excel('filename.xlsx') with the filename as a string argument.
  3. Final Answer:

    df.to_excel('data.xlsx') -> Option A
  4. Quick Check:

    Excel export syntax = df.to_excel(filename) [OK]
Hint: Use to_excel() with filename string to save Excel files [OK]
Common Mistakes:
  • Using non-existent methods like save_excel()
  • Missing quotes around filename
  • Using write_excel() which is not a pandas method
3. What will be the output of the following code?
import pandas as pd
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df.to_json('output.json')
with open('output.json') as f:
    content = f.read()
print(content)
medium
A. {"A":{"0":1,"1":2},"B":{"0":3,"1":4}}
B. [{"A":1,"B":3},{"A":2,"B":4}]
C. Error: to_json() requires extra parameters
D. Empty file content

Solution

  1. Step 1: Understand default to_json() output format

    By default, to_json() saves data in 'columns' orientation. The default is 'columns', which creates a nested JSON with columns as keys.
  2. Step 2: Check printed JSON content

    The printed content shows keys 'A' and 'B' with nested indices and values, matching the default 'columns' orientation JSON.
  3. Final Answer:

    {"A":{"0":1,"1":2},"B":{"0":3,"1":4}} -> Option A
  4. Quick Check:

    Default to_json() output = nested JSON by columns [OK]
Hint: Default to_json() saves nested JSON by columns [OK]
Common Mistakes:
  • Expecting list of dicts JSON without setting orient
  • Thinking to_json() needs extra parameters
  • Assuming file is empty after saving
4. You try to save a DataFrame df to Excel using df.to_excel('file.xlsx') but get an error: ModuleNotFoundError: No module named 'openpyxl'. What is the best way to fix this?
medium
A. Change the filename to .csv and use to_csv()
B. Install the openpyxl library using pip
C. Use df.to_excel() without filename
D. Restart Python without any changes

Solution

  1. Step 1: Understand the error cause

    The error means pandas needs the 'openpyxl' library to write Excel files but it is not installed.
  2. Step 2: Fix by installing the required library

    Installing 'openpyxl' with pip install openpyxl will provide the needed support for Excel export.
  3. Final Answer:

    Install the openpyxl library using pip -> Option B
  4. Quick Check:

    Missing Excel engine error = install openpyxl [OK]
Hint: Install openpyxl to export Excel files with pandas [OK]
Common Mistakes:
  • Ignoring the missing library error
  • Trying to save without filename
  • Switching to CSV without reason
5. You have a DataFrame df and want to export it to both CSV and HTML formats in one script. Which code snippet correctly does this?
hard
A. df.save(['data.csv', 'data.html'])
B. df.to_csv('data.csv', 'data.html')
C. df.to_csv('data.csv')\ndf.to_html('data.html')
D. df.export(['data.csv', 'data.html'])

Solution

  1. Step 1: Understand pandas export methods

    pandas provides separate methods for each format: to_csv() for CSV and to_html() for HTML.
  2. Step 2: Use both methods separately

    Calling df.to_csv('data.csv') and then df.to_html('data.html') saves the DataFrame in both formats correctly.
  3. Final Answer:

    df.to_csv('data.csv')\ndf.to_html('data.html') -> Option C
  4. Quick Check:

    Separate calls for each format = correct export [OK]
Hint: Call to_csv() and to_html() separately to export both [OK]
Common Mistakes:
  • Trying to pass multiple filenames to one method
  • Using non-existent export() or save() methods
  • Expecting one method to handle multiple formats
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard