在pandas dataframe中,根据数值对数据项进行颜色编码。

6

我有一个数据框,包含一些数据(姓名、金额X、金额Y和“差异”列,用于减去X和Y的金额。

我想要将“差异”为正数的单元格染成绿色,并将负数的染成红色。是否可能实现?


这并不是一件容易的事情。你可能需要在使用to_html方法时进行创意,为正/负值分配类,然后使用一些CSS? - DataSwede
你的大小是多少? - Moritz
2个回答

5

1

我还没有尝试过在pandas中进行样式设置,但是可以尝试使用Python包装器ggplot(https://github.com/yhat/ggpy)。

# One might get some dependency errors in-regards to pandas-timeseries 
# module and statsmodels
# if that happens try, pip install --upgrade statsmodels 

sudo pip install ggplot
# Assuming one has a dataframe then,
from ggplot import *
# internally, it will figure out the unique values for VariableC and 
# define a color change automatically
ggplot(aes(x='variableA', y='variableB', color='VariableC'), 
        data=data_df) + geom_point()

希望这对你有所帮助...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接