0

I'm wondering what the most efficient (and hopefully concise) way to map values in a column is. I.e., I have a list of mappings I want for a given column (this just a simple example):

df = pd.DataFrame({"col": ["A", "D", "B", "B", "C", "K", "A"]})
mappings = {"A": "X", "B": "Y", "C": "Z"}
# What I'm currently doing
for k, v in mappings.items():
    df.loc[df.col == k, "col"] = v

I know loc is fast, but is there a more concise way to do mappings that is also fully vectorized?

Nick K
  • 23
  • 3

0 Answers0