英文:
Pandas styler gradient vmin vmax from another column
问题
我有一个名为df的数据框,看起来如下。
参数 M1 M2 M3 相对 Vmin 相对 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
我想创建一个数据框样式,但是不想使用轴的最小/最大值,我想使用“相对 Vmin”和“相对 Vmax”列中的值。
所以在我的上面的例子中,相对于那些列,“参数 P1”的测量值都相对较低,即使 M2 和 M3 比 M1 高。我想要样式反映这一点。
除了逐行迭代,还有其他方法可以做到吗?
示例代码:
import pandas as pd
df = pd.DataFrame(data={'参数': ['P1', 'P2', 'P3'],
'M1': [1.5, 2.2, 3.2],
'M2': [3.1, 4.1, 3.5],
'M3': [3.0, 4.2, 3.4],
'相对 Vmin':[1,2,2.5],
'相对 Vmax':[100,6,6.5]})
测量值 = ['M1', 'M2', 'M3']
df.style.background_gradient(axis=1, subset=measurements)
# ^ 我不想要这个,我想要做类似于 vmin=df['相对 Vmin'], vmax=df['相对 Vmax']
英文:
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']
答案1
得分: 1
I may be wrong but I think that a for-loop is inevitable here:
s = df.style
for r, (v1,v2) in enumerate(zip(df["Relative Vmin"], df["Relative Vmax"])):
s = s.background_gradient(
subset=pd.IndexSlice[r, measurements],
vmin=v1,
vmax=v2
)
Output:
Update:
If you want to interpolate the measurements, you can use:
out = df.copy()
out[measurements] = np.array(
[np.interp(m, [v1, v2], [0, 1])
for m, v1, v2 in zip(
df[measurements].to_numpy(),
df["Relative Vmin"].to_numpy(),
df["Relative Vmax"].to_numpy())]
)
tmp = out.style.background_gradient(subset=measurements, axis=1)
s = df.style.use(tmp.export())
Output:
英文:
I may be wrong but I think that a for-loop is inevitable here :
s = df.style
for r, (v1,v2) in enumerate(zip(df["Relative Vmin"], df["Relative Vmax"])):
s = s.background_gradient(
subset=pd.IndexSlice[r, measurements],
vmin=v1,
vmax=v2
)
Output :
Update :
If you want to interpolate the measurements, you can use :
out = df.copy()
out[measurements] = np.array(
[np.interp(m, [v1, v2], [0, 1])
for m, v1, v2 in zip(
df[measurements].to_numpy(),
df["Relative Vmin"].to_numpy(),
df["Relative Vmax"].to_numpy())]
)
tmp = out.style.background_gradient(subset=measurements, axis=1)
s = df.style.use(tmp.export())
Output :
答案2
得分: 1
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)
英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论