[Python] Pandas Excel Write Issue with Boolean Values: 'True' Becomes 'TRUE'

Discussão em 'Python' iniciado por Stack, Abril 15, 2025.

  1. Stack

    Stack Membro Participativo

    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:

    1. Run the code to generate the Excel file.
    2. Open the file with Excel
    3. In column 4 (a new column), manually type True
    4. Compare column 3 with 4
    5. 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.

    Continue reading...

Compartilhe esta Página