1
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.

1 Answer 1

1

First, your data dictionary should be corrected. All nan should be set as np.nan. Something for you have to think about next time when you create data for others to try!

Second, you added the style code with the function which you didn't really show what the function does. You didn't included the function detail with this.

Assuming your first function to style works, the problem with the function is that you're checking this for each column. Since you're using .apply without axis, it defaults to 0 i.e. columns - one column at a time. Since your first column seems to be string, it can't convert the value to string; hence, the error.

So, to fix this, your __highlight_pvalue__ function needs to be changed to check only for pvalue.

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: np.nan, 1: np.nan, 37: np.nan, 40: np.nan, 43: np.nan, 5: np.nan, 6: np.nan, 12: np.nan, 15: np.nan, 18: np.nan, 21: np.nan, 24: np.nan, 27: np.nan, 30: np.nan, 33: np.nan, 36: np.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'}}
data = pd.DataFrame(data)

def __highlight_pvalue__(x):
        
    if x.p_value == '<0.001' or float(x.p_value) < 0.05:
        return ['font-weight: bold'] * x.shape[0] # number of colums
    else: 
        return ''
          
      

The resulting dataframe

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

this is perfect. Sorry about the data and thank you for the suggestion. I appreciate it and will keep in mind the next time I post something over. Your solution works perfectly. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.