How to write Pandas bool column as True/False instead of 1/0 to XLSX
Problem:
When writing a pandas XLSX, bool
-type columns are shown are 0
for False
or 1
for True
.
df = pd.DataFrame([
{"a": True},
{"a": False},
{"a": True},
{"a": False},
{"a": False},
])
with pd.ExcelWriter("out.xlsx", engine="xlsxwriter") as writer:
df.to_excel(writer)
Solution
Map the column to a string column with your desired value before exporting:
df["a"] = df["a"].map({True: "True", False: "False"})
Full example code:
df = pd.DataFrame([
{"a": True},
{"a": False},
{"a": True},
{"a": False},
{"a": False},
])
df["a"] = df["a"].map({True: "True", False: "False"})
with pd.ExcelWriter("out.xlsx", engine="xlsxwriter") as writer:
df.to_excel(writer)