Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

dataframe color cells

import pandas as pd
import numpy as np

def highlight_max(s):
    is_max = s == s.max()
    return ['background-color: red' if v else '' for v in is_max]

df.style.apply(highlight_max)
Comment

dataframe color cells

import pandas as pd

df = pd.DataFrame({
    "name":         ["alan","beth","charlie","david", "edward"],
    "age" :         [34,    12,     43,      32,      77],
    "num_children": [1,     0,      2,       1,       6],
    "num_pets":     [1,     0,      1,       2,       0],
    "bank_balance": [100.0, 10.0,   -10.0,   30.0,    30.0]})

def more_children_or_more_pets_background(row):    

    highlight = 'background-color: lightcoral;'
    default = ''

    # must return one string per cell in this row
    if row['num_children'] > row['num_pets']:
        return [highlight, default]
    elif row['num_pets'] > row['num_children']:
        return [default, highlight]
    else:
        return [default, default]

df.style.apply(more_children_or_more_pets_background, subset=['num_children', 'num_pets'], axis=1)
Comment

PREVIOUS NEXT
Code Example
Python :: custom jupyter notebook 
Python :: numpy aray map values with dictionary 
Python :: create a dictionary from a list python 
Python :: How to join two dataframes by 2 columns so they have only the common rows? 
Python :: strp datetime 
Python :: random 0 or 1 python 
Python :: try except json decode error 
Python :: uninstall python using powershell 
Python :: encrypt string with key python 
Python :: python3 shebang line 
Python :: python check if list contains 
Python :: cv2 copy image 
Python :: split at the second occurrence of the element python 
Python :: picasa 
Python :: python optional parameters 
Python :: python copy to clipboard command 
Python :: creating numpy array using zeros 
Python :: pow python 
Python :: separating tuple in pandas 
Python :: python import timezone 
Python :: how to make python file executable 
Python :: with open as file python 
Python :: Python NumPy broadcast_to() Function Example 
Python :: python loop opening file from directory 
Python :: python assert is not null 
Python :: pandas dataframe to series 
Python :: pil.jpegimageplugin.jpegimagefile to image 
Python :: how to get the link of an image in selenium python 
Python :: python next item in list 
Python :: pands correlation matrix to dataframe 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =