2

My DataFrame looks something like this

d1 = pd.DataFrame({"Country":['xx','xx','xy','xz','xz'],
               "year":['y1','y2','y1','y1','y2'],
               "population":[100,200,120,140,190]})

What would be the best way to highlight all the distinct countries in a particular column in different colors? That would mean, the three distinct countries in the "country" column ("xx","xy","xz") needs to be highlighted in different colors each.

EDIT : These values are generated dynamically and can be different each time. So I am guessing hard coding the colors based on the values is not an option

  • 1
    this has already been answered in SO. Check this out, https://stackoverflow.com/questions/43596579/how-to-use-pandas-stylers-for-coloring-an-entire-row-based-on-a-given-column – Alex Rajan Samuel Mar 04 '22 at 06:05
  • 1
    @AlexRajanSamuel I think the question seems to talk about how to highlight a row if the values exceed a certain threshold. I don't understand how its related to this question. – Whiteflames Mar 04 '22 at 06:12

1 Answers1

2

You can use a list of colors and make a mapping dictionary from the list of countries, then use style.applymap:

import matplotlib

colors = dict(zip(d1['Country'].unique(),
                  (f'background-color: {c}' for c in matplotlib.colors.cnames.values())))

d1.style.applymap(colors.get, subset=['Country'])

output:

enter image description here

mozway
  • 194,879
  • 13
  • 39
  • 75