'str'对象没有属性'str'或'extract'

问题描述 投票:-4回答:1

这是我正在使用Pandas分析的第一批数据表之一。我想清除“位置”列,以使不同的城市显示为:乔治亚州亚特兰大纽约,纽约等等enter image description here

def get_city(address):
    pattern = r'(.+\,\w.+)\w.+)'
    return address.str.extract(pattern,flags=re.I)

location = df['location']        

location.apply(get_city)
location.head()

但是,在提取和使用正则表达式时出现以下错误。

AttributeError: 'str' object has no attribute 'str'

或在提取前删除.str时得到:

AttributeError: 'str' object has no attribute 'extract'

您能帮忙吗?

python regex string pandas extract
1个回答
0
投票

Series.apply()每个值

传递给get_city()函数。您无法在单个字符串值上使用Series.str....函数,您没有完整的序列。
© www.soinside.com 2019 - 2024. All rights reserved.