0

I get Dataframe by pivot_table:

df = pd.pivot_table(df, values=['labor_costs'],
                         index=['Division', 'Perfomer'],
                         columns=['completed on time'], aggfunc=[np.sum, len], fill_value=0)

enter image description here

How can I highlight the row by condition: on schedule == 0 and overdue == 0 like table above?

irina_ikonn
  • 69
  • 1
  • 1
  • 5
  • Does this answer your question? [How to use Pandas stylers for coloring an entire row based on a given column?](https://stackoverflow.com/questions/43596579/how-to-use-pandas-stylers-for-coloring-an-entire-row-based-on-a-given-column) – Seon Aug 09 '23 at 14:47
  • does this answer your question ?https://stackoverflow.com/questions/60102093/highlight-row-in-pandas-when-condition-match – vegan_meat Aug 09 '23 at 14:54
  • I use function 'pivot_table' and I can't apply df['overdue'] or df['on shedule'] in my condition – irina_ikonn Aug 09 '23 at 15:06

1 Answers1

1

The code snippet below highlights rows based on the condition you described.

enter image description here

You can customise the text and background colours, font weight and size, etc.

import numpy as np
np.random.seed(0)

df = pd.DataFrame({'schedule': np.random.choice([0, 12, 23], 11),
                   'overdue': np.random.choice([0, 132, 323], 11)})

def style_overdue(x):
    highlight = ['color:black;background-color:lawngreen;font-weight:bold']
    no_highlight = [''] #['color:white;background-color:black'] #can be customised
    condition = (x.schedule == 0) & (x.overdue == 0)
    return (highlight if condition else no_highlight) * len(x)
(
    df
    .style
    .apply(style_overdue, axis=1)
)
some3128
  • 1,430
  • 1
  • 2
  • 8