gpt4 book ai didi

Python - 系列对象是可变的 - 地址解析

转载 作者:行者123 更新时间:2023-11-28 22:13:43 30 4
gpt4 key购买 nike

我正在尝试在数据框中创建一个新列,该列将从字符串中解析出地址。尝试执行此操作时,我收到以下错误:

("'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

最佳答案

MCVE

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

解决X/Y问题

df['cleansed_address'] = df['descrsched'].str.findall(pat).str[0]

关于Python - 系列对象是可变的 - 地址解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53750897/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com