1

I have the following dataset frame1

Color Item
Red Shirt
White Shoes
Yellow Shirt
Green Shoes

I want to set all the colors for Shoes item to be "Blue", I use map



x = {"Shoes": "Blue"}
fr1["Color"] = fr1["Item"].map(x)

I expected the following result

Color Item
Red Shirt
Blue Shoes
Yellow Shirt
Blue Shoes

Instead I got this

Color Item
NaN Shirt
Blue Shoes
NaN Shirt
Blue Shoes

1 Answers1

0

Either only use the rows with a valid key with boolean indexing:

x = {"Shoes": "Blue"}
m = fr1["Item"].isin(list(x))
fr1.loc[m, "Color"] = fr1.loc[m, "Item"].map(x)

Or fillna:

fr1["Color"] = fr1["Item"].map(x).fillna(fr1["Color"])
mozway
  • 194,879
  • 13
  • 39
  • 75