0

Let's say I have a dataframe like this one:

     Col 1      Col 2          Time 1
0     A          A_1          24:00:00
1     A          A_2          18:00:00
2     B          B_1          36:00:00
3     B          B_2          78:00:00

I want to make a condition in the "Time 1" Column and if it's >72:00:00 fill it with red color. And then to export it to csv and have an output like this one:

Output

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
user14073111
  • 647
  • 5
  • 14
  • Hey @Tomerikoo, I saw that one and didn't solve my problem. – user14073111 May 16 '21 at 16:21
  • 1
    If you saw this one it means that you are aware of `style.apply` and if it didn't help you then it means you tried something that didn't work. In that case it is better to post a [mre] of your attempt so we can help with your problem. Simply saying I saw this one and it didn't help doesn't help us to help you – Tomerikoo May 16 '21 at 16:23

1 Answers1

2

Firstly create a function that check condition:

def highlight(s):
    res=s.str.split(':').str[0].astype(int)>72
    return ['background-color: red' if v else '' for v in res]

Finally use style.apply():

df=df.style.apply(highlight,subset=['Time 1'])

Output of df:

enter image description here

Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41
  • Hey @anuragdabas, i tried your solution in my dataframe and it is working good if the values in "Time 1" column are <100. If i have a value there that is for example 150:00:00. it wont make it red. How can i solve this? – user14073111 May 16 '21 at 15:27
  • @user14073111 Updated answer....kindly have a look **:)** – Anurag Dabas May 16 '21 at 16:49