我喜欢使用background_gradient,因为它可以帮助我以 Excel 的方式查看数据帧。
但我想知道是否有一种方法可以将颜色映射到另一个数据框中的数字。
例如,我热衷于使用 zscores 数据框为数据框着色,这样我就可以快速看到异常值的值。
A = pd.DataFrame(np.random.randn(6, 3), columns=['a', 'b', 'c'])
B = pd.DataFrame(np.random.randn(6, 3), columns=['a', 'b', 'c'])
A.style.background_gradient(???)
我想知道如何使用background_gradient
以便它使用数据帧 B 中的值来设置 A 的样式。
除了更改之外我没有看到其他方法背景渐变代码用于将样式从一个数据框传输到另一个数据框,即
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import colors
def b_g(s, cmap='PuBu', low=0, high=0):
# Pass the columns from Dataframe A
a = A.loc[:,s.name].copy()
rng = a.max() - a.min()
norm = colors.Normalize(a.min() - (rng * low),
a.max() + (rng * high))
normed = norm(a.values)
c = [colors.rgb2hex(x) for x in plt.cm.get_cmap(cmap)(normed)]
return ['background-color: %s' % color for color in c]
B.style.apply(b_g,cmap='PuBu')
输出 :
![Dataframes](https://i.stack.imgur.com/YwvNm.png)
希望能帮助到你
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)