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 !

TLP
  • 109
  • 9
  • 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. – DYZ Jul 04 '18 at 23:15

1 Answers1

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

Would this help?

Bartek Malysz
  • 922
  • 5
  • 14
  • 37