data = {'Rest (N=11570)': {4: '7369 (62.28%)', 1: '7016 (59.98%)', 37: '734 (6.14%)', 40: '9829 (79.09%)', 43: '37 (2.42%)', 5: '3080 (25.29%)', 6: '1273 (9.43%)', 12: '6992 (62.61%)', 15: '777 (5.47%)', 18: '332 (2.10%)', 21: '7 (3.44%)', 24: '2013 (14.39%)', 27: '290 (1.81%)', 30: '2048 (14.98%)', 33: '9 (3.85%)', 36: '353 (2.15%)', 351: '0.08 [0.05 - 0.14]', 354: '0.08 [0.05 - 0.13]', 357: '0.08 [0.04 - 0.14]', 168: '4.00 [3.72 - 4.30]', 171: '3.96 [3.62 - 4.27]', 174: '4.22 [3.92 - 4.50]', 177: '3.81 [3.44 - 4.12]', 180: '3.93 [3.60 - 4.20]'}, 'p_value': {4: '<0.001', 1: '0.005', 37: '0.056', 40: '<0.001', 43: '<0.001', 5: '<0.001', 6: '<0.001', 12: '<0.001', 15: '<0.001', 18: '<0.001', 21: '<0.001', 24: '<0.001', 27: '<0.001', 30: '<0.001', 33: '<0.001', 36: '<0.001', 351: '0.366', 354: '<0.001', 357: '0.008', 168: '0.012', 171: '0.004', 174: '0.220', 177: '0.047', 180: '0.025'}, 'ks_score': {4: nan, 1: nan, 37: nan, 40: nan, 43: nan, 5: nan, 6: nan, 12: nan, 15: nan, 18: nan, 21: nan, 24: nan, 27: nan, 30: nan, 33: nan, 36: nan, 351: '0.02', 354: '0.05', 357: '0.03', 168: '0.03', 171: '0.03', 174: '0.02', 177: '0.02', 180: '0.03'}}
I have this data for which I need to perform some styling using pandas. I'm trying to set text bold for the row which has p_value < 0.05 or p_value == '<0.001'. But I can't figure out how to do this. I'm reading the documentation from https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html.
def __highlight_pvalue__(x):
return ['font-weight:bold' if p == '<0.001' or float(p) < 0.05 else p for p in x]
call the styling function:
df.style.apply(__add_categorical_header_row__, row_idxs = row_indices, row_labels = row_labels, axis = None).apply(__highlight_pvalue__)
the function __add_categorical_header_row__ works fine but when I add __highlight_pvalue__ function, that's when I get the following error. I understand what the error means, but can't figure out how to solve it.
could not convert string to float: '7369 (62.28%)'
I also tried putting try except inside the __hightlight_pvalue__ function without success.
