to_excel() expects an Excel file extension like .xlsx, not .csv.
Step 2: Understand file format mismatch
Saving with .csv extension causes confusion; .csv is for comma-separated files, not Excel format.
Final Answer:
File extension should be .xlsx, not .csv -> Option A
Quick Check:
to_excel needs .xlsx extension [OK]
Hint: Use .xlsx extension with to_excel() [OK]
Common Mistakes:
Using .csv extension with to_excel()
Thinking to_excel() cannot save DataFrames
Assuming missing imports cause error
5. You have a JSON file with nested data and want to convert it into a flat pandas DataFrame. Which approach best uses flexible I/O to handle this real-world data?
hard
A. Use pandas.read_csv() directly on the JSON file
B. Manually open the JSON file and parse it line by line without pandas
C. Use pandas.read_json() with the 'lines=True' parameter and then normalize nested fields
D. Convert JSON to Excel first, then read with pandas.read_excel()
Solution
Step 1: Understand JSON reading in pandas
pandas.read_json() can read JSON files; 'lines=True' helps with line-delimited JSON.
Step 2: Flatten nested JSON data
After reading, pandas.json_normalize() can flatten nested fields into columns for easy analysis.
Final Answer:
Use pandas.read_json() with the 'lines=True' parameter and then normalize nested fields -> Option C
Quick Check:
read_json + normalize = best for nested JSON [OK]
Hint: Use read_json with normalization for nested data [OK]
Common Mistakes:
Trying to read JSON with read_csv()
Ignoring nested structure flattening
Converting formats unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.