Consider the following Python code using pandas. What will be the content of the Excel file output.xlsx after running this?
import pandas as pd df = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [25, 30]}) df.to_excel('output.xlsx', index=False)
Check the index parameter in to_excel.
Setting index=False means the row numbers are not saved in the Excel file. So only the data columns and rows appear.
Given this code, how many sheets will the Excel file contain?
import pandas as pd with pd.ExcelWriter('multi_sheet.xlsx') as writer: pd.DataFrame({'A': [1, 2]}).to_excel(writer, sheet_name='Sheet1') pd.DataFrame({'B': [3, 4]}).to_excel(writer, sheet_name='Sheet2')
Each to_excel call with a different sheet_name adds a sheet.
Two to_excel calls with different sheet names create two sheets in the Excel file.
What error will this code raise when trying to write a DataFrame to Excel?
import pandas as pd df = pd.DataFrame({'X': [1, 2]}) df.to_excel('file.xlsx', sheet_name='Data', engine='nonexistent_engine')
Check if the engine name is valid for pandas Excel writing.
The engine parameter must be a valid Excel writer engine. 'nonexistent_engine' is invalid, so pandas raises a ValueError.
You want to add new rows to an existing Excel sheet without deleting the old data. Which code snippet achieves this?
Appending means adding data without deleting existing content.
Using mode='a' opens the file in append mode, and if_sheet_exists='overlay' allows adding rows without deleting existing ones.
Consider a DataFrame with a MultiIndex. What will the Excel output look like if you call to_excel without index=False?
Think about how pandas represents MultiIndex in Excel output.
By default, pandas writes each level of the MultiIndex as separate columns on the left side in Excel, preserving the index structure.