I have a dataframe df which looks like following.
Parameter M1 M2 M3 Relative Vmin Relative Vmax
0 P1 1.5 3.1 3.0 1.0 100.0
1 P2 2.2 4.1 4.2 2.0 6.0
2 P3 3.2 3.5 3.4 2.5 6.5
I want to create a dataframe styler but instead of using the min/max of the axis, I would like to use the values in the Relative Vmin and Relative Vmax columns.
So in my example above, the Parameter P1 measurements are all "relatively" low with respect to those columns, even though M2 and M3 are higher than M1. I would like the styling to reflect this.
Is there any way to do this besides iterating through each row one-by-one?
Sample code:
import pandas as pd
df = pd.DataFrame(data={'Parameter': ['P1', 'P2', 'P3'],
'M1': [1.5, 2.2, 3.2],
'M2': [3.1, 4.1, 3.5],
'M3': [3.0, 4.2, 3.4],
'Relative Vmin':[1,2,2.5],
'Relative Vmax':[100,6,6.5]})
measurements = ['M1', 'M2', 'M3']
df.style.background_gradient(axis=1, subset=measurements)
# ^ I don't want this, I want to do something like vmin=df['Relative Vmin'], vmax=df['Relative Vmax']
For complex color mapping you should use gmap
and calculate the colour map yourself.
df = pd.DataFrame({
"M1": [1.5, 2.2, 3.2],
"M2": [3.1, 4.1, 3.5],
"M3": [3.0, 4.2, 3.4],
"Vmin": [1.0, 2.0, 2.5],
"Vmax": [100.0, 6.0, 6.5]
})
df["M1_gmap"] = (df["M1"] - df["Vmin"]) / (df["Vmax"] - df["Vmin"])
df["M2_gmap"] = (df["M2"] - df["Vmin"]) / (df["Vmax"] - df["Vmin"])
df["M3_gmap"] = (df["M3"] - df["Vmin"]) / (df["Vmax"] - df["Vmin"])
df.style.background_gradient(subset=["M1", "M2", "M3"], axis=None, gmap=df[["M1_gmap", "M2_gmap", "M3_gmap"]].values)