This question is a continuation to my previously asked question (Color a Pandas DataFrame column based on distinct values)
So basically, i want to highlight the entire rows of a DataFrame, based on the distinct values in a particular column (In this case, its the "Country" column)
Here is how i am trying to achieve this
d1 = pd.DataFrame({"Country":['xx','xx','xy','xz','xz'],
"year":['y1','y2','y1','y1','y2'],
"population":[100,200,120,140,190]})
import matplotlib
colors = dict(zip(d1['Country'].unique(),
(f'background-color: {c}' for c in matplotlib.colors.cnames.values())))
def test_check(df):
for key in colors:
# print(key)
if key in df['Country']:
return colors.get(key)
else:
pass
d2 = d1.style.apply(test_check, axis=1)
but i end up with this error
ValueError: Function <function test_check at 0x0000024D090E98B0> returned the wrong shape. Result has shape: (5,) Expected shape: (5, 3)
I want my output to be something like this. What is the best way to do this?