KeyError: ‘Styler.apply and .applymap are not compatible with non-unique index or columns.’
解决方法:reset_index(drop=True)重置索引,因为有时候对dataframe做处理后索引可能是乱的。
def color_negative(val, color):
color = color if val == "same" else "black"
weight = "bold"
return ";".join([f"color:{color}", f"font-weight:{weight}"])
new_result_data.reset_index(drop=True).style.applymap(
color_negative, subset=["result"], color="#FFA07A"
).to_excel(
f"xxx.xlsx",
index=False,
encoding="utf-8",
engine="openpyxl",
)
参考链接1
参考链接2
apply()是一种让函数作用于DataFrame中行或列的操作。 applymap()是一种让函数作用于DataFrame每一个元素的操作。



