B. The filename must be a full path, not just 'file.csv'
C. DataFrame df is not defined correctly
D. The to_csv function cannot exclude the index
Solution
Step 1: Check the parameter value for index
The index parameter expects a boolean: True or False. Using No is invalid and causes an error.
Step 2: Validate other parts of the code
The filename 'file.csv' is valid, and the DataFrame is correctly defined. The to_csv function supports excluding the index.
Final Answer:
The parameter index should be False, not No -> Option A
Quick Check:
Use True/False for boolean parameters [OK]
Hint: Boolean parameters need True or False, not No/Yes [OK]
Common Mistakes:
Using 'No' or 'Yes' instead of False/True
Thinking filename must be full path
Assuming to_csv can't exclude index
5.
You have a DataFrame df with columns ['Name', 'Score']. You want to save it as a CSV file named results.csv using a semicolon ; as the separator, UTF-8 encoding, and without saving the row index. Which code snippet correctly does this?
hard
A. df.to_csv('results.csv', sep=';', encoding='utf-8')
B. df.to_csv('results.csv', separator=';', encoding='utf8')
C. df.to_csv('results.csv', sep=';', encoding='utf-8', index=False)
D. df.to_csv('results.csv', sep=';', encoding='utf-8', no_index=True)
Solution
Step 1: Set separator and encoding parameters correctly
The separator parameter is sep, and encoding should be 'utf-8'. df.to_csv('results.csv', sep=';', encoding='utf-8', index=False) uses both correctly.
Step 2: Exclude the index if not needed
To exclude row numbers, use index=False. df.to_csv('results.csv', sep=';', encoding='utf-8', index=False) includes this correctly; df.to_csv('results.csv', sep=';', encoding='utf-8') misses it, so index will be saved.
Final Answer:
df.to_csv('results.csv', sep=';', encoding='utf-8', index=False) -> Option C
Quick Check:
Use sep and encoding with index=False to customize CSV [OK]
Hint: Use sep=';' and encoding='utf-8' with index=False [OK]
Common Mistakes:
Using 'separator' instead of 'sep'
Forgetting index=False to exclude row numbers
Using no_index=True which is invalid
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.