I want to highlight dataframe cells contained in a specific list, to get better performance, I want this only happens to specific column.
- I understand we should set
df.stylebackground-color: yellowattribute https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html - we should use
df.applyrather thandf.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)