Search code examples
pythonhtmlpandasdataframepandas-styles

Highlight pandas dataframe cells contained in a list


I want to highlight dataframe cells contained in a specific list, to get better performance, I want this only happens to specific column.

  1. I understand we should set df.style background-color: yellow attribute https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html
  2. we should use df.apply rather than df.applymap, later one is element-wise https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.apply.html#pandas.DataFrame.apply

Sample code is cleaned up as follows, but does not generate highlighted items at all. Can anyone help me with this, thanks.

import pandas as pd
import os
import webbrowser

def write_data_to_file(filename, content):

    file = open(filename, mode='w')
    file.write(content)
    file.close()


def highlight_hot_color(col):

    hot_color = ['red', 'yellow', 'orange']
    check = [item in hot_color for item in col]
    return ['background-color: yellow' if v else '' for v in check]

if __name__ == '__main__':
    df = pd.DataFrame([['Allen', 'red', 20], ['Tom', 'yellow', 30], ['Jack', 'blue', 40], ['Bob', 'grey', 50]], 
        columns=['name', 'color', 'age'])
    df.style.apply(highlight_hot_color, subset=['color'])

    html = df.to_html(index=False)
    file_name = 'test.html'
    path_name = os.path.abspath(file_name)
    url = 'file://' + path_name
    write_data_to_file(path_name, html)
    webbrowser.open(url)

Solution

  • Problem resolved after many hours investigation, hope this can help others.

    df = pd.DataFrame([['Allen', 'red', 20], ['Tom', 'yellow', 30], ['Jack', 'blue', 40], ['Bob', 'grey', 50]],
    columns=['name', 'color', 'age'])
    df_result = df.style.apply(highlight_hot_color, subset=['color'])
    
    with open('test.html','w') as f:
    f.write(df_result.render())