我正在尝试在数据帧中创建新列,该新列将从字符串中解析出地址。尝试执行此操作时,出现以下错误:
("'Series' objects are mutable, thus they cannot be hashed", u'occurred at index pk')
在此网站上我还看到了与此类似的其他问题,但还不太了解它如何应用于我的代码:
import usaddress, re, pyodbc
import pandas as pd
conn = pyodbc.connect("DSN=TEST;UID=test;PWD=test")
sql = "select top 10 pk, address from test..test"
df = pd.read_sql(sql,conn)
pattern = re.compile(".+\\b[0-9]{5}\\b")
def extract(pat):
print pat
test = pattern.findall(pat)
return str(test[0])
i = 0
for i in df.iterrows():
df[i]['cleansed_address'] = df.apply(lambda x: extract(df[i]['descrsched']))
i+=1
答案 0 :(得分:4)
df = pd.DataFrame([[1, 2,], [3, 4]])
df
# This is a tuple (index value, Series object that represents row)
# |
# v
for i in df.iterrows():
print(df[i])
# ^
# |
# This is you trying to tell Pandas to use a tuple
# in which the second element is a Series as a reference for a column name
df['cleansed_address'] = df['descrsched'].str.findall(pat).str[0]