I have a data frame as shown below.
summary_FR =pd.concat([Chip_Cur_Summary_funct_mode2,Noise_Summary_funct_mode2,VCM_Summary_funct_mode2,Sens_Summary_funct_mode2,Vbias_Summary_funct_mode2,vcm_delta_Summary_funct_mode2,THD_FUN_M2,F_LOW_FUNC_Summary_mode2,OSC_FUNC_Summary_mode2,FOSC_FUNC_Summary_mode2,VREF_CP_FUNC_Summary_mode2,Summary_PSRR_1KHz_funct_mode2,Summary_PSRR_20Hzto20KHz_funct_mode2]) summary_FR cell_hover = { # for row hover use <tr> instead of <td> 'selector': 'td:hover', 'props': [('background-color', '#ffffb3')] } index_names = { 'selector': '.index_name', 'props': 'font-style: italic; color: darkgrey; font-weight:normal;' } headers = { 'selector': 'th:not(.index_name)', 'props': 'background-color: #000066; color: white;' } summary_FR.style.set_table_styles([cell_hover, index_names, headers])
In the first row I need to compare SPEC_TYP with max. In the 2nd row I need to compare SPEC_MAX with max.In the 3rd row SPEC_TYP with max and in some other cases I need to compare SPEC_MIN with min ,SPEC_MAX with max and so on.
I searched in SO and google to see how can I do this but all the results I obtained show comparing two columns directly. This is not possible in my case .Because in my case some time I need to compare SPEC_TYP with max and in other cases SPEC_MAX with max and so on.
May I know how to approach this condition.
When I am adding more items I am getting an error ” unexpected indent”
def compare_to(row, what, to, prop=''): return np.where(row.index == to, prop if row[to] > row[what] else '', '') summary_FR.style.apply(compare_to, what='SPEC_TYP', to='max', prop='background-color:red', axis=1, subset=(['Chip_Cur(uAmp)'],)) .apply(compare_to, what='SPEC_MAX', to='max', prop='background-color:red', axis=1, subset=(['Noise[uVrms]'],)) .apply(compare_to, what='SPEC_MAX', to='max', prop='background-color:red', axis=1, subset=(['Output_Common_Mode_Voltage[V]'],))
Advertisement
Answer
You can apply different styles to different subset
s:
import pandas as pd import numpy as np df = pd.DataFrame({'SPEC_TYP': [1, 2, 3], 'SPEC_MAX': [2, 4, 6], 'max': [3, 5, 3]}, index=list('abc')) def compare_to(row, what, to, prop=''): return np.where(row.index == to, prop if row[to] > row[what] else '', '') df.style.apply(compare_to, what='SPEC_TYP', to='max', prop='background-color:tomato', axis=1, subset=(['a', 'c'],)) .apply(compare_to, what='SPEC_MAX', to='max', prop='background-color:orange', axis=1, subset=(['b'],))