简体   繁体   中英

python pandas coloring of rows

I have this code that colors every time slot differently, so it is easier to navigate. The problem is, sometimes not all the slots will be populated and then i get the same color on the next slot and it will be hard to distinguish which slot is which. how can i make this code more elegant and make python decide when there is a next slot to change the color. i want to have those colors changed "binary".

def EPP_Break_boja(row):
    color = '#F5EEF8'
    if row.values[0] == '06:40':
        color = '#EBF5FB  '
    elif row.values[0] == '07:10':
        color = '#FEF9E7  '
    elif row.values[0] == '07:40':
        color = '#EBF5FB  '
    elif row.values[0] == '08:10':
        color = '#FEF9E7  '
    elif row.values[0] == '08:40':
        color = '#EBF5FB  '
    elif row.values[0] == '09:10':
        color = '#FEF9E7  '
    elif row.values[0] == '09:40':
        color = '#EBF5FB  '
    elif row.values[0] == '10:10':
        color = '#FEF9E7  '
    elif row.values[0] == '10:40':
        color = '#EBF5FB  '
    elif row.values[0] == '11:10':
        color = '#FEF9E7  '
    elif row.values[0] == '11:40':
        color = '#EBF5FB  '
    elif row.values[0] == '12:10':
        color = '#FEF9E7  '
    elif row.values[0] == '12:40':
        color = '#EBF5FB  '
    elif row.values[0] == '13:10':
        color = '#FEF9E7  '
    elif row.values[0] == '13:40':
        color = '#EBF5FB  '
    elif row.values[0] == '14:10':
        color = '#FEF9E7  '
    elif row.values[0] == '14:40':
        color = '#EBF5FB  '
    elif row.values[0] == '15:10':
        color = '#FEF9E7  '
    elif row.values[0] == '15:40':
        color = '#EBF5FB  '
    elif row.values[0] == '16:10':
        color = '#FEF9E7  '
    elif row.values[0] == '16:40':
        color = '#EBF5FB  '
    elif row.values[0] == '17:10':
        color = '#FEF9E7  '
    elif row.values[0] == '17:40':
        color = '#EBF5FB  '
    elif row.values[0] == '18:10':
        color = '#FEF9E7  '
    elif row.values[0] == '18:40':
        color = '#EBF5FB  '
    elif row.values[0] == '19:10':
        color = '#FEF9E7  '
    elif row.values[0] == '19:40':
        color = '#EBF5FB  '
    elif row.values[0] == '20:10':
        color = '#FEF9E7  '
    elif row.values[0] == '20:40':
        color = '#EBF5FB  '
    elif row.values[0] == '21:10':
        color = '#FEF9E7  '
    elif row.values[0] == '21:40':
        color = '#EBF5FB  '
    elif row.values[0] == '22:10':
        color = '#FEF9E7  '
    elif row.values[0] == '22:40':
        color = '#EBF5FB  '
    elif row.values[0] == '23:10':
        color = '#FEF9E7  '
    elif row.values[0] == '23:40':
        color = '#EBF5FB  '
    elif row.values[0] == 'Thursday 12-October-2017':
        color = '#FDEDEC  '
    elif row.values[0] == 'Thursday 13-October-2017':
        color = '#EAFAF1'
    return ['background-color: %s' % color]*len(row.values)


dataframe_spotovi = dataframe_spotovi.style.apply(EPP_Break_boja, axis=1)
dataframe_spotovi

what about the following approach:

In [112]: colors=['#EBF5FB  ','#FEF9E7  ']

In [113]: df
Out[113]:
    Time title     x
0  06:40     a  2011
1  06:40     b  2010
2  07:10     c  2000
3  08:10     d  1983

In [114]: df.assign(col=np.take(colors, df.groupby('Time').ngroup() % 2))
Out[114]:
    Time title     x        col
0  06:40     a  2011  #EBF5FB
1  06:40     b  2010  #EBF5FB
2  07:10     c  2000  #FEF9E7
3  08:10     d  1983  #EBF5FB

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM