# Pandas : pd.style ###### tags: `python` ``` from collections import namedtuple import pandas as pd Product = namedtuple('Product', ['name', 'price', 'taste']) WeekData = namedtuple('WeekData', ['week', 'row_data']) data = [ WeekData(week='W321', row_data=[Product(name='A', price=10, taste='good'), Product(name='B', price=10, taste='excellent'), Product(name='C', price=8, taste='fair')]), WeekData(week='W322', row_data=[Product(name='A', price=12, taste='good'), Product(name='B', price=15, taste='excellent'), Product(name='C', price=16, taste='fair')]), WeekData(week='W323', row_data=[Product(name='A', price=14, taste='good'), Product(name='B', price=20, taste='excellent'), Product(name='C', price=22, taste='fair')]) ] df_data = [] for item in data: row = [p.price for p in item.row_data] + [p.taste for p in item.row_data] df_data.append(row) df = pd.DataFrame(df_data, index=pd.Index(['A', 'B', 'C'], name='Group_Name'), columns=pd.MultiIndex.from_product([['Price', 'Taste'], ['W321', 'W322', 'W323']], names=['CAL', 'Weeks'])) # Get the "Taste" data from the DataFrame taste_data = df.loc[:, ('Taste', slice(None))] print(taste_data) def style_negative(v): if isinstance(v, int) and v < 15: return 'color:red;' elif isinstance(v, int) and 20 > v > 15: return 'color:blue;' else: return '' df_styled = df.style.applymap(style_negative, subset=pd.IndexSlice[:, 'Price']) \ .applymap(lambda v: 'opacity: 20%;' if isinstance(v, str) else '', subset=pd.IndexSlice[:, 'Taste']) ``` ![](https://hackmd.io/_uploads/H1j-mUiw2.png) ``` from collections import namedtuple import pandas as pd Product = namedtuple('Product', ['name', 'price', 'taste']) WeekData = namedtuple('WeekData', ['week', 'row_data']) data = [ WeekData(week='W321', row_data=[Product(name='A', price=10, taste='good'), Product(name='B', price=10, taste='excellent'), Product(name='C', price=8, taste='fair')]), WeekData(week='W322', row_data=[Product(name='A', price=12, taste='good'), Product(name='B', price=15, taste='excellent'), Product(name='C', price=16, taste='fair')]), WeekData(week='W323', row_data=[Product(name='A', price=14, taste='good'), Product(name='B', price=20, taste='excellent'), Product(name='C', price=22, taste='fair')]) ] df_data = [] for item in data: row = [p.price for p in item.row_data] + [p.taste for p in item.row_data] df_data.append(row) df = pd.DataFrame(df_data, index=pd.Index(['A', 'B', 'C'], name='Group_Name'), columns=pd.MultiIndex.from_product([['Price', 'Taste'], ['W321', 'W322', 'W323']], names=['CAL', 'Weeks'])) # Get the "Taste" data from the DataFrame taste_data = df.loc[:, ('Taste', slice(None))] print(taste_data) def style_negative(v, props=''): if isinstance(v, int) and v < 15: props = 'color:red;' elif isinstance(v, int) and 20 > v > 15: props = 'color:blue;' else: props = '' return props df_styled = df.style.applymap(style_negative) \ .applymap(lambda v: 'opacity: 80%;' if isinstance(v, int) else '') cell_hover = { 'selector': 'td:hover', 'props': [('background-color', '#ffffb3')] } index_names = { 'selector': '.index_name', 'props': [('font-style', 'italic'), ('color', 'darkgrey'), ('font-weight', 'normal')] } headers = { 'selector': 'th:not(.index_name)', 'props': [('background-color', '#000066'), ('color', 'white')] } detail = { 'selector': 'th.col_heading', 'props': ['text-align: center;'], 'selector': 'th.col_heading.level0', 'props': 'font-size: 1.5em;', 'selector': 'td', 'props': [('text-align','center'), ('font-weight', 'bold;')], "selector": '',"props": [("border", "2px solid grey")] } df_styled.set_table_styles([cell_hover, index_names, headers, detail]) df_styled = df_styled.background_gradient() html = df_styled.render() text_file = open("index.html", "w") text_file.write(html) text_file.close() ``` ![](https://hackmd.io/_uploads/HJD5X8ov3.png) ## Reference * https://pandas.pydata.org/docs/user_guide/style.html