Reputation: 17164
I was able to highlight 'object' dtype, but when I tried the same thing for 'category', it fails.
How to highlight both 'object' and 'category' dtype in pandas style?
# code
import numpy as np
import pandas as pd
import seaborn as sns
df = sns.load_dataset('titanic')
df1 = df.dtypes.to_frame()
# this only highlights 'object'
df1.style.apply(lambda x: ["background: salmon"
if v =='object' else "" for v in x], axis = 1)
# this fails
df1.style.apply(lambda x: ["background: salmon"
if v in ['object','category'] else "" for v in x], axis = 1)
But I want both object and category be highlighted.
Upvotes: 1
Views: 174
Reputation: 150785
A quick fix is to compare the string representation:
# this fails
df1.style.apply(lambda x: ["background: salmon"
if str(v) in ['object','category'] else "" for v in x], axis = 1)
Output:
Upvotes: 3