What is the pandas function to export a DataFrame to a CSV file?
The function is DataFrame.to_csv(). It saves the DataFrame as a CSV file which can be opened in spreadsheet programs.
Click to reveal answer
beginner
How do you export a pandas DataFrame to an Excel file?
Use DataFrame.to_excel(). It writes the DataFrame to an Excel file with .xlsx extension.
Click to reveal answer
beginner
Which pandas method exports a DataFrame to a JSON file?
Use DataFrame.to_json(). It saves the DataFrame in JSON format, useful for web data exchange.
Click to reveal answer
intermediate
What parameter in to_csv() helps to avoid saving the row index?
Set index=False in to_csv() to prevent saving the DataFrame's row index in the file.
Click to reveal answer
intermediate
Why might you choose to export data in multiple formats?
Different formats serve different needs: CSV for simple tables, Excel for formatted sheets, JSON for web apps. Exporting multiple ways makes data usable in many places.
Click to reveal answer
Which pandas method exports a DataFrame to a CSV file?
Ato_json()
Bto_excel()
Cto_csv()
Dto_html()
✗ Incorrect
to_csv() is used to export DataFrames to CSV files.
How do you prevent the row index from being saved in a CSV export?
Aindex=False
Bcolumns=False
Cheader=False
Dindex=True
✗ Incorrect
Setting index=False in to_csv() skips saving the row index.
Which file format is best for sharing data with web applications?
AJSON
BExcel
CCSV
DTXT
✗ Incorrect
JSON is widely used for web data exchange because it is easy to parse in web apps.
What pandas method would you use to export a DataFrame to an Excel file?
Ato_csv()
Bto_excel()
Cto_json()
Dto_pickle()
✗ Incorrect
to_excel() exports DataFrames to Excel files.
Which parameter controls whether column headers are saved in CSV export?
Aindex
Bsep
Ccolumns
Dheader
✗ Incorrect
The header parameter controls if column names are saved in the CSV file.
Explain how to export a pandas DataFrame to CSV, Excel, and JSON formats. Include key parameters to control the output.
Think about the method names and how to exclude the index or headers.
You got /5 concepts.
Why is it useful to export data in multiple formats? Give examples of when you might use CSV, Excel, and JSON.
Consider how different people or programs might want to use the data.
You got /4 concepts.
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
Step 1: Recall pandas export methods
pandas provides specific methods to export data, such as to_csv() for CSV files.
Step 2: Match method to file type
The method to_csv() is the standard way to save DataFrames as CSV files.
Final Answer:
to_csv() -> Option D
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
Step 1: Identify the correct pandas method for Excel export
The method to save a DataFrame to Excel is to_excel().
Step 2: Check syntax correctness
The correct syntax is calling df.to_excel('filename.xlsx') with the filename as a string argument.
Final Answer:
df.to_excel('data.xlsx') -> Option A
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
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.
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.
Final Answer:
{"A":{"0":1,"1":2},"B":{"0":3,"1":4}} -> Option A
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
Step 1: Understand the error cause
The error means pandas needs the 'openpyxl' library to write Excel files but it is not installed.
Step 2: Fix by installing the required library
Installing 'openpyxl' with pip install openpyxl will provide the needed support for Excel export.
Final Answer:
Install the openpyxl library using pip -> Option B