亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何根據單元格中的值使用數據框和 python 突出顯示行?

如何根據單元格中的值使用數據框和 python 突出顯示行?

www說 2023-04-11 16:06:55
我想根據單元格上的值突出顯示數據框和 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)

輸出是:

http://img1.sycdn.imooc.com//643515560001d70402390250.jpg

要導出到 Excel,請執行以下操作:

df = df.style.apply(highlight_sentiment, axis=1)
df.to_excel("my_file.xlsx")


查看完整回答
反對 回復 2023-04-11
?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

可能是您status在調用函數時沒有發送輸入參數。

def_test_twtr_preds.style.apply(highlight_sentiment("positive"),axis =1)


查看完整回答
反對 回復 2023-04-11
  • 2 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號