Pandas styler gradient从另一列获取vmin和vmax。

huangapple go评论85阅读模式
英文:

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:

Pandas styler gradient从另一列获取vmin和vmax。

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:

Pandas styler gradient从另一列获取vmin和vmax。

英文:

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 :

Pandas styler gradient从另一列获取vmin和vmax。

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 :

Pandas styler gradient从另一列获取vmin和vmax。

答案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)

Pandas styler gradient从另一列获取vmin和vmax。

huangapple
  • 本文由 发表于 2023年5月18日 07:00:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定