1

I need to replace parts of abbreviated addresses with the full address in a new csv but keep running into an error. How do I fix this?

1234 Edison Ln -----------> 1234 Edison Lane

4589 Stack Overflow Dr -----------> 4589 Stack Overflow Drive

import pandas as pd

mycsv = pd.read_csv('addressescsv')
mycsv['Address'] = str.replace({mycsv['Address']: {'Ln': 'Lane','Dr': 'Drive'}})

mycsv.to_csv('newAddressescsv', index=False)

Traceback:

Traceback (most recent call last):
File "C:\movingalong.py", line 8, in <module>
File "C:\Users\Programs\Python\Python36-32\lib\site-
packages\pandas\core\generic.py", line 831, in __hash__
' hashed'.format(self.__class__.__name__))
TypeError: 'Series' objects are mutable, thus they cannot be hashed
Jake Wagner
  • 786
  • 2
  • 12
  • 29

1 Answers1

3

You could use DataFrame.replace

df = pd.DataFrame({'Address':['Ln', 'Dr', 'High']})
print df.replace({'Address' :{'Ln': 'Lane','Dr': 'Drive'}})

Output

       Address
0   Lane
1  Drive
2   High

Since you are looking for partial match you may want to try this

import re
import pandas as pd 

df = pd.DataFrame({'Address':['City Ln', 'New Dr', 'Ln']})
rep = {'Ln': 'Lane','Dr': 'Drive'}
regex = re.compile(r"\b((%s)\S*)\b" %"|".join(rep.keys()), re.I)

def dictionary_lookup(match):
    return rep[match.group(2)]

def ReplaceStr(value):
    NewValue = regex.sub(dictionary_lookup, value)
    return NewValue


df["New Address"] = df["Address"].map(lambda x:ReplaceStr(x))
print df

output

   Address New Address
0  City Ln   City Lane
1   New Dr   New Drive
2       Ln        Lane

Inspired by https://stackoverflow.com/a/32191354/6626530

Community
  • 1
  • 1
Shijo
  • 9,313
  • 3
  • 19
  • 31