Home > Software design >  Assign color to unique item in pandas df
Assign color to unique item in pandas df

Time:09-27

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'])]

CodePudding user response:

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)))
  • Related