pandas - 样式 - 使用其他数据框的背景渐变 [英] Pandas - Style - Background Gradient using other dataframe

查看:35
本文介绍了 pandas - 样式 - 使用其他数据框的背景渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢使用 background_gradient,因为它可以帮助我以 excel 的方式查看我的数据框.
但我想知道是否有办法将颜色映射到另一个数据框中的数字.
例如,我热衷于使用 zscores 数据框为数据框着色,以便我可以快速查看异常值的值.

  A = pd.DataFrame(np.random.randn(6,3),column = ['a','b','c'])B = pd.DataFrame(np.random.randn(6, 3), columns=['a', 'b', 'c'])A.style.background_gradient(???)

我想知道如何使用 background_gradient 以便它使用数据帧 B 中的值来设置样式 A.

解决方案

除了改变

希望能帮到你

I like using the background_gradient as it helps me look at my dataframes in an excel way.
But I'm wondering if I there is a way I could map the colors to the figures in another dataframe.
For example, something I am keen to do is to color the dataframe using a dataframe of zscores so i can see quickly the value of outliers.

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(???)

I'm wondering how to use background_gradient so that it uses the values in the dataframe B to style A.

解决方案

I don't see a different method other than altering the background_gradient code for transferring style from one dataframe to other i.e

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')

Output :

Hope it helps

这篇关于 pandas - 样式 - 使用其他数据框的背景渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆