簡體   English   中英

根據值對熊貓數據框中的數據項進行顏色設置

[英]Color data items in a pandas dataframe based on value

我有一個數據幀,其中包含一些數據(名稱,amount_x,amount_y和“ diff”列,它們減去了X和Y的數量。

我想給單元格上色,其中“ diff”為正數(並使其變為綠色),而為負數時,使其為紅色)?

這可能嗎?

從Pandas版本0.17.1開始

您可以使用style.applymap這樣的行

df.style.applymap(color, subset=['diff'])

更多信息和示例在這里: https : //pandas.pydata.org/pandas-docs/stable/style.html

還沒有嘗試過熊貓的樣式,但是,可以嘗試一下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()

希望這很有幫助...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM