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:
- Search
df1['address']for occurrences of words indf2['common_abbrev']and replace withdf2['usps_abbrev']. - 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:
Since I've already given the
dtypefordf2, why am I receiving the error?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.