根据值在pandas数据框中着色数据项

时间:2014-12-09 23:42:41

标签: python pandas visualization

我有一个包含一些数据的数据框(名称,amount_x,amount_y和" diff"列,它减去了数量X和Y.

我想为细胞上色,其中"差异"是一个正数(并使它们变为绿色),它是负数,使其成为红色)?

这可能吗?

2 个答案:

答案 0 :(得分:0)

从Pandas版本0.17.1开始

您可以将 style.applymap 与此类行

一起使用
df.style.applymap(color, subset=['diff'])

此处有更多信息和示例: https://pandas.pydata.org/pandas-docs/stable/style.html

答案 1 :(得分:0)

没有尝试过pandas中的样式,但是,可以试试ggplot( https://github.com/yhat/ggpy )的python包装器,

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

希望这有用......