替换单个列的部分

时间:2017-02-06 18:10:53

标签: python pandas

我需要用新csv中的完整地址替换部分缩写地址,但仍然会遇到错误。我该如何解决这个问题?

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

4589 Stack Overflow Dr -----------> 4589堆栈溢出驱动器

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

1 个答案:

答案 0 :(得分:3)

您可以使用DataFrame.replace

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

输出

       Address
0   Lane
1  Drive
2   High

由于您正在寻找部分匹配,因此您可能需要尝试此

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

输出

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

https://stackoverflow.com/a/32191354/6626530启发