1

I am using Python 3.7 and have a large data frame that includes a column of addresses, like so:

c = ['420 rodeo drive', '22 alaska avenue', '919 franklin boulevard', '39 emblem alley', '67 fair way']
df1 = pd.DataFrame(c, columns = ["address"])

There is also a dataframe of address abbrevations and their usps version. An example:

x = ['avenue', 'drive', 'boulevard', 'alley', 'way']
y = ['aly', 'dr', 'blvd', 'ave', 'way']
df2 = pd.DataFrame(list(zip(x,y)), columns = ['common_abbrev', 'usps_abbrev'], dtype = "str")

What I would like to do is as follows:

  1. Search df1['address'] for occurrences of words in df2['common_abbrev'] and replace with df2['usps_abbrev'].
  2. Return the transformed df1.

To this end I have tried in a few ways what appears to be the canonical strategy of df.str.replace() like so:

df1["address"] = df1["address"].str.replace(df2["common_abbrev"], df2["usps_abbrev"])

However, I get the following error:

`TypeError: repl must be a string or callable`.

My question is:

  1. Since I've already given the dtype for df2, why am I receiving the error?

  2. How can I produce my desired result of:

             address
    0       420 rodeo dr
    1      22 alaska ave
    2  919 franklin blvd
    3      39 emblem aly
    4        67 fair way
    

Thanks for your help.

1 Answer 1

1

First create a dict using zip. Then use Series.replace:

In [703]: x = ['avenue', 'drive', 'boulevard', 'alley', 'way']
     ...: y = ['aly', 'dr', 'blvd', 'ave', 'way']

In [686]: d = dict(zip(x, y))

In [691]: df1.address = df1.address.replace(d, regex=True)

In [692]: df1
Out[692]: 
             address
0       420 rodeo dr
1      22 alaska aly
2  919 franklin blvd
3      39 emblem ave
4        67 fair way
Sign up to request clarification or add additional context in comments.

5 Comments

d = dict(zip(x, y)) might be better
@MustafaAydın Yes, updated my answer. Thanks for suggestion.
@MayankPorwal thanks this is clean and intuitive.
@MayankPorwal one more question: what is it about dict that converts my datatype to strings?
Also, some of the 'ave' replacements are adding an 'e' to the new string, so 'avenue' becomes 'avee' or 'aveee' instead of 'ave'. Is there a reason why?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.