1. 程式人生 > 其它 >pandas AttributeError: ‘Styler‘ object has no attribute ‘style‘解決方法

pandas AttributeError: ‘Styler‘ object has no attribute ‘style‘解決方法

技術標籤:Pythonpythonpandas

問題:使用pandas更改格式遇到AttributeError: ‘Styler’ object has no attribute ‘style’

原因:使用一次.style.applymap()或者style.apply()函式後DataFrame物件就變成了Styler物件了

在這裡插入圖片描述

解決方法:

把兩次使用合在一起
之前的錯誤程式碼

data_df = data_df.style.applymap(low_buy_price_red, subset=['離買點的百分點'])
data_df = data_df.style.apply(green_descend, subset=['選股至今'])

data_df.to_excel(save_path, index=None)
data_df = pd.read_excel(save_path)

更改後的正確程式碼:

data_df = data_df.style.applymap(low_buy_price_red, subset=['離買點的百分點']).apply(green_descend, subset=['選股至今'])

大功告成!