Apply Multiple Styles to a data frame specific column

淺唱寂寞╮ 提交于 2021-01-28 17:41:17

问题


I image that this might be a complex issue. I want to have five different colors for a specific column. cld_ght in the dataframe. Here is the dataframe:

    icao msg_type              time    dt  ddd  ff    gg flt_cat   vis  cld_hgt cld_type present_wx vis_obc
0   KLAX  ROUTINE  2019-10-14 00:53  1:00  260  10 -9999     VFR  10.0     9999     9999       None   -9999
1   KLAX  ROUTINE  2019-10-14 01:53  1:00  240   9 -9999     VFR  10.0     9999     9999       None   -9999
2   KLAX  ROUTINE  2019-10-14 02:53  1:00  260   6 -9999     VFR  10.0     9999     9999       None   -9999
3   KLAX  ROUTINE  2019-10-14 03:53  1:00  250   5 -9999     VFR  10.0     9999     9999       None   -9999
4   KLAX  ROUTINE  2019-10-14 04:53  1:00  240   4 -9999     VFR  10.0     9999     9999       None   -9999
5   KLAX  ROUTINE  2019-10-14 05:53  1:00  250   5 -9999     VFR  10.0     9999     9999       None   -9999

I developed a code like this:

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)    

I get this error

 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')

I'm not sure I'm doing this right

So would you split it up like this

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 in VFR_condition:
        if val00:
            color = '#00b050'
        results.append(f"color : {color}") 
    for val01 in MVFR_condition:
        if val01:
            color = '#FFFF00'
        results.append(f"color : {color}") 
    for val02 in IFR_condition:
        if val02:
            color = '#FF0000'
        results.append(f"color : {color}") 
    for val03 in LIFR_condition:
        if val03:
            color = '#FFA500'
        results.append(f"color : {color}") 
    for val04 in VLIFR_condition:
        if val04:
            color = '#9400D3'
        results.append(f"color : {color}") 
    return(results)  

If I do it like this I would need to to add multiple result arrays. Would you split it into different functions ?


回答1:


I think you can do with 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'])

Output:



来源:https://stackoverflow.com/questions/58442947/apply-multiple-styles-to-a-data-frame-specific-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!