Search code examples
pythonpandascategorical-data

how to assign categorical values according to numbers in a column of a dataframe?


I have a data frame with a column 'score'. It contains scores from 1 to 10. I want to create a new column "color" which gives the column color according to the score.

For e.g. if the score is 1, the value of color should be "#75968f", if the score is 2, the value of color should be "#a5bab7". i.e. we need colors ["#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2","#f1d4Af", "#dfccce", "#ddb7b1", "#cc7878", "#933b41", "#550b1d"] for scores [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] respectively.

Is it possible to do this without using a loop? Let me know in case you have a problem understanding the question.


Solution

  • Use Series.map with dictionary generated by zipping both lists or if need range by length of list colors is possible use enumerate:

    df = pd.DataFrame({'score':[2,4,6,3,8]})
    
    colors = ["#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2","#f1d4Af", 
              "#dfccce", "#ddb7b1", "#cc7878", "#933b41", "#550b1d"]
    scores = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    
    df['new'] = df['score'].map(dict(zip(scores, colors))) 
    
    df['new1'] = df['score'].map(dict(enumerate(colors, 1)))   
    print (df)
       score      new     new1
    0      2  #a5bab7  #a5bab7
    1      4  #e2e2e2  #e2e2e2
    2      6  #dfccce  #dfccce
    3      3  #c9d9d3  #c9d9d3
    4      8  #cc7878  #cc7878