How to set a cell not column or row in a dataframe with color?

梦想与她 提交于 2021-02-20 04:12:54

问题


I have a dataframe with table style that I created :

 tableyy = final.style.set_table_attributes('border="" class = "dataframe table table-hover table-bordered"').set_precision(10).render()

I have go through this Coloring Cells in Pandas , Conditionally change background color of specific cells, Conditionally format Python pandas cell, and Colour cells in pandas dataframe, I still not able to set a cell with color not the whole dataframe without any condition. Anyone have any ideas, I try this color code for one months already so hope can receive advise from anyone of you guys, thanks.


回答1:


Try this:

data =  pd.DataFrame(np.random.randn(6, 6), columns=list('ABCDEF'))

def highlight_cells(x):
    df = x.copy()
    #set default color
    #df.loc[:,:] = 'background-color: papayawhip' 
    df.loc[:,:] = '' 
    #set particular cell colors
    df.iloc[0,0] = 'background-color: red'
    df.iloc[1,1] = 'background-color: orange'
    df.iloc[2,2] = 'background-color: yellow'
    df.iloc[3,3] = 'background-color: lightgreen'
    df.iloc[4,4] = 'background-color: cyan'
    df.iloc[5,5] = 'background-color: violet'
    return df 

t = data.style.apply(highlight_cells, axis=None)
t




回答2:


@kathir: you may find many examples here. My example below is a graphical over-kill but summarizes some features. There is a 'border' property but it did not work in my environment (Jupyter).

import pandas as pd
import numpy as np

def highlight_single_cells(x):
    df = x.copy()
    df.loc[:,:] = '' 
    #set particular cells colors
    df.iloc[0,0] = 'background-color: red'
    df.iloc[1,1] = 'background-color: orange'
    df.iloc[2,2] = 'background-color: yellow'
    df.iloc[3,3] = 'background-color: lightgreen'
    df.iloc[4,4] = 'background-color: cyan'
    df.iloc[5,5] = 'background-color: violet'
    return df

def bold_single_cells(x):
    df = x.copy()
    df.loc[:,:] = '' 
    #set particular cells bold
    df.iloc[0,0] = 'font-weight: bold'
    df.iloc[1,1] = 'font-weight: bold'
    df.iloc[2,2] = 'font-weight: bold'
    df.iloc[3,3] = 'font-weight: bold'
    df.iloc[4,4] = 'font-weight: bold'
    df.iloc[5,5] = 'font-weight: bold'
    return df

def color_negative_red(val):
    #--- colors the + and the - values
    color = 'blue' if val < 0 else 'black'
    return 'color: %s' % color

def highlight_max(s):
    #---- highlight the maximum in a column
    is_max = s == s.max()
    return ['background-color: chartreuse' if v else '' for v in is_max]

#--- generate data ----
x = np.linspace(0.0, 0.998, 6)
xx,yy = np.meshgrid(x,x)
df =  pd.DataFrame(xx*yy-0.3, columns=list('ABCDEF'))

#--- apply the styles you want ----
df.style\
    .set_caption('My pandas sheet with a caption')\
    .background_gradient(cmap='Pastel2')\
    .applymap(color_negative_red)\
    .apply(highlight_max)\
    .apply(highlight_single_cells, axis=None)\
    .set_properties(**{'max-width': '80px',\
                       'font-size': '15pt', \
                       'border-color': 'black'})\
    .apply(bold_single_cells, axis=None)\
    .set_precision(3)\



来源:https://stackoverflow.com/questions/43599572/how-to-set-a-cell-not-column-or-row-in-a-dataframe-with-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!