How to bold certain values in a column of a dataframe before transferring it to excel?

Question:

I want to bold only Fruits and Vegetables under product in the below dataframe using python and transfer the data to excel, in the excel the Fruits and Vegetables should be in bold. Appreciate your help!

df:
      Code    Product     Limit   Value
0   3A68185     Fruits  0.6     0.000000
1   3A68185     Apple   0.6     0.000000
3   3B22979     Fruits  3.5     0.430146
2   3B22979     Apple   3.5     0.430146
4   3B22979     Orange  0.0     0.000000
6   3C67260     Fruits  3.0     1.123774
5   3C67260     Apple   3.0     1.123774
7   3C71601     Vegetables  15.0    0.000000
8   3C71601     Tomato  15.0    0.000000
14  3C78910     Fruits  2.0     1.187282
15  3C78910     Apple   2.0     1.187282
16  3C82861     Fruits  64.0    0.560864
17  3C82861     Apple   15.0    0.000000
18  3C82861     Orange  49.0    0.560864
21  3D11357     Vegetables  26.0    0.000000
19  3D11357     Tomato  25.5    0.000000
20  3D11357     Onion   0.5     0.000000
23  3D51126     Vegetables  15.0    0.000000
24  3D51126     Tomato  14.5    0.000000
22  3D51126     Onion   0.5     0.000000
26  3E20062     Vegetables  1.0     0.000000
25  3E20062     Onion   1.0     0.000000
10  3E45212     Fruits  5.0     0.000000
9   3E45212     Apple   5.0     0.000000
13  3E45212     Vegetables  36.0    0.000000
11  3E45212     Tomato  35.5    0.000000
12  3E45212     Onion   0.5     0.000000

Expected Output:
Code Product Limit Value
0 3A68185 Fruits 0.6 0.000000
1 3A68185 Apple 0.6 0.000000
3 3B22979 Fruits 3.5 0.430146
2 3B22979 Apple 3.5 0.430146
4 3B22979 Orange 0.0 0.000000
6 3C67260 Fruits 3.0 1.123774
5 3C67260 Apple 3.0 1.123774
7 3C71601 Vegetables 15.0 0.000000
8 3C71601 Tomato 15.0 0.000000
14 3C78910 Fruits 2.0 1.187282
15 3C78910 Apple 2.0 1.187282
16 3C82861 Fruits 64.0 0.560864
17 3C82861 Apple 15.0 0.000000
18 3C82861 Orange 49.0 0.560864
21 3D11357 Vegetables 26.0 0.000000
19 3D11357 Tomato 25.5 0.000000
20 3D11357 Onion 0.5 0.000000
23 3D51126 Vegetables 15.0 0.000000
24 3D51126 Tomato 14.5 0.000000
22 3D51126 Onion 0.5 0.000000
26 3E20062 Vegetables 1.0 0.000000
25 3E20062 Onion 1.0 0.000000
10 3E45212 Fruits 5.0 0.000000
9 3E45212 Apple 5.0 0.000000
13 3E45212 Vegetables 36.0 0.000000
11 3E45212 Tomato 35.5 0.000000
12 3E45212 Onion 0.5 0.000000

Asked By: SGC

||

Answers:

You can use this:

def bold_categ(cat):
    return ['font-weight: bold' if i in ["Vegetables","Fruits"] else () for i in cat]

df.style.apply(bold_categ)
Answered By: Hamzah
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.