Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Assign color to unique item in pandas df

I’m hoping to assign the same color to each unique item in a pandas df. The number of unique items can vary so I’m hoping to use a dynamic approach.

Using below, I want to assign the same color to each unique string in Item. At the moment, it’s just using the length of the df.

d = ({                
    'Val' : [5,1,4,-2,8,4,3,10],   
    'Item' : ['X', 'Y', 'A', 'B', 'X','D', 'X', 'E'],                      
    })

df = pd.DataFrame(data = d)

df['colors'] = px.colors.qualitative.Alphabet[:len(df['Item'])]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use Series.unique to get the unique value of Item column

df['colors'] = df['Item'].map(dict(zip(df['Item'].unique(),
                                       px.colors.qualitative.Alphabet[:len(df['Item'].unique())])))
print(df)

   Val Item   colors
0    5    X  #AA0DFE
1    1    Y  #3283FE
2    4    A  #85660D
3   -2    B  #782AB6
4    8    X  #AA0DFE
5    4    D  #565656
6    3    X  #AA0DFE
7   10    E  #1C8356

Or you can use value_counts to get the count each item in Item and change count to color

df['colors'] = df['Item'].map(df['Item'].value_counts()
                              .pipe(lambda s: pd.Series(px.colors.qualitative.Alphabet[:len(s)], index=s.index)))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading