I have a DataFrame where one column contains the string values "True" and "False". However, when I export the DataFrame to Excel using pd.to_excel and open the file in Excel, these values appear as Excel logical values (TRUE/FALSE in uppercase).
As a result, comparing the value "True" from pandas with the Excel "TRUE" returns False
This issue doesn't happen when exporting to CSV; everything remains as plain text.
import pandas as pd
d = {'col1': [1, 2], 'col2': [3, 4], 'col3': ['True','False']}
df = pd.DataFrame(data=d)
writer = pd.ExcelWriter(r'H:\ExcelTest2.xlsx', engine = 'xlsxwriter')
df.to_excel(writer, index=False, sheet_name='Sheet1')
writer.save()
To reproduce:
- Run the code to generate the Excel file.
- Open the file with Excel
- In column 4 (a new column), manually type
True - Compare column 3 with 4
- Notice the answer is always
False
My question:
How can I export data from pandas to Excel without pandas converting "True"/"False" strings into Excel boolean values (TRUE/FALSE)?
I want the values to remain text exactly as they are in the DataFrame.