Home > database >  Create a new python dataframe column based on a dictionary of strings lookup
Create a new python dataframe column based on a dictionary of strings lookup

Time:07-03

I have the following dataframe:

import pandas as pd    
df = pd.DataFrame({"bpstage": ["Normal", "Stage1", "Elevated", "Normal"]})

which lists a classification of blood pressure readings as strings, and I also have a strings dictionary listing all possible blood pressure classifications and for each an Hex code of the color I want to plot them to:

bp_stages = {
    "normal": "#aecd55",
    "elevated": "#fcec4f",
    "stage1": "#f4b93f",
    "stage2": "#ad451d",
    "crisis": "#8c1e1b",
}

my goal is to add a new column/series to the dataframe, say we call it bpcolor, and for each bpstage column row choose the corresponding color from the dictionary and fill that value in the corresponding bpcolor row.

Tried the following

df["bpcolor"] = df["bpstage"]
df["bpcolor"].map(bp_stages)

but only get NaNs. Found similar solutions here with numeric values and those seem to work. Why is the case with strings not? Thank you

CodePudding user response:

Problem is that word doesn't match between dictionary key and column value because of uppercase and lowercase, you can lowercase all values in bpstage column then map

df["bpcolor"] = df["bpstage"].str.lower().map(bp_stages)
print(df)

    bpstage  bpcolor
0    Normal  #aecd55
1    Stage1  #f4b93f
2  Elevated  #fcec4f
3    Normal  #aecd55
  • Related