我想根據單元格上的值突出顯示數據框和 csv 文件中的記錄?我試圖創建一個函數并將此函數應用于數據框,但它沒有突出顯示任何記錄。輸出必須是:代碼:def_test_twtr_preds= pd.read_excel(path,names=col_names) def highlight_sentiment(status): if status == "Positive": return ['background-color: yellow'] else: return ['background-color: white'] def_test_twtr_preds.style.apply(highlight_sentiment,axis =1)錯誤在哪里??
2 回答

忽然笑
TA貢獻1806條經驗 獲得超5個贊
這是一個有效的解決方案(用合成數據演示):
df = pd.DataFrame({"a": [1, 2, 3], "status": ["Negative", "Positive", "Positive"]})
def highlight_sentiment(row):
if row["status"] == "Positive":
return ['background-color: yellow'] * len(row)
else:
return ['background-color: white'] * len(row)
df.style.apply(highlight_sentiment, axis=1)
輸出是:
要導出到 Excel,請執行以下操作:
df = df.style.apply(highlight_sentiment, axis=1) df.to_excel("my_file.xlsx")

qq_花開花謝_0
TA貢獻1835條經驗 獲得超7個贊
可能是您status
在調用函數時沒有發送輸入參數。
def_test_twtr_preds.style.apply(highlight_sentiment("positive"),axis =1)
添加回答
舉報
0/150
提交
取消