0

I have a df where each row has a code that indicates a department.

On the other hand, I have a dictionary in which each code corresponds to a region name (a region is constituted of multiple departments).

I thought of a loop to put the value in a new column that indicates the region.

Here is what I thought would work:

for r in df:
    dep = r["dep"].astype(str)
    r["region"] = dep_dict.get(dep)

But the only thing I get is "string indices must be integers". Do you people know how could I make it work ? Or if I should take a totally different route (like joining) ?

Thanks ! 🙏

1
  • The r in for r in df is a column header (which is a string, in your case). So, r["dep"] is an attempt to index a string with a string. Commented Jul 4, 2018 at 23:15

1 Answer 1

0
df.region =  df.dep.apply(lambda x: dep_dict[x])

Would this help?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.