我認為這可能是一個復雜的問題。我想為特定列設置五種不同的顏色。數據框中的 cld_ght。這是數據框: icao msg_type time dt ddd ff gg flt_cat vis cld_hgt cld_type present_wx vis_obc0 KLAX ROUTINE 2019-10-14 00:53 1:00 260 10 -9999 VFR 10.0 9999 9999 None -99991 KLAX ROUTINE 2019-10-14 01:53 1:00 240 9 -9999 VFR 10.0 9999 9999 None -99992 KLAX ROUTINE 2019-10-14 02:53 1:00 260 6 -9999 VFR 10.0 9999 9999 None -99993 KLAX ROUTINE 2019-10-14 03:53 1:00 250 5 -9999 VFR 10.0 9999 9999 None -99994 KLAX ROUTINE 2019-10-14 04:53 1:00 240 4 -9999 VFR 10.0 9999 9999 None -99995 KLAX ROUTINE 2019-10-14 05:53 1:00 250 5 -9999 VFR 10.0 9999 9999 None -9999我開發了這樣的代碼:def cloud_height_style_function(val): VFR_condition = val>3000 MVFR_condition = 1000<=val<=3000 IFR_condition = 500<=val<1000 LIFR_condition = 200<=val<500 VLIFR_condition = val<200 results = [] for val00,val01,val02,val03,val04 in zip(VFR_condition,MVFR_condition,IFR_condition,LIFR_condition,VLIFR_condition): if val00: color = '#00b050' elif val01: color = '#FFFF00' elif val02: color = '#FF0000' elif val03: color = '#FFA500' elif val04: color = '#9400D3' results.append(f"color : {color}") return(results) highlighted=df.style.apply(cloud_height_style_function,subset=['cld_hgt']).render()with open('myhtml.html','w') as f: f.write(highlighted) 我收到這個錯誤 ValueError: ('The truth value of a Series is ambiguous. Use a.empty,a.bool(), a.item(), a.any() or a.all().', 'occurred at index cld_hgt')我不確定我做對了所以你會像這樣分開嗎def cloud_height_style_function(val): VFR_condition = val>3000 MVFR_condition = 1000<=val<=3000 IFR_condition = 500<=val<1000 LIFR_condition = 200<=val<500 VLIFR_condition = val<200 results = [] 如果我這樣做,我需要添加多個結果數組。你會把它分成不同的功能嗎?
1 回答

米脂
TA貢獻1836條經驗 獲得超3個贊
我認為你可以這樣做pd.cut:
def cloud_height_style_function(vals):
return pd.cut(vals,
[-np.inf,200,500,1000,3000, np.inf], # need to review the bin a bit
labels=[f'color: {c}' for c in ['#9400D3','#FFA500','#FF0000','#FFFF00','#00b050']]
)
df.style.apply(cloud_height_style_function,subset=['cld_hgt'])
輸出:
添加回答
舉報
0/150
提交
取消