这是我正在使用Pandas分析的第一批数据表之一。我想清除“位置”列,以使不同的城市显示为:乔治亚州亚特兰大纽约,纽约等等
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'
您能帮忙吗?
Series.apply()
将每个值
get_city()
函数。您无法在单个字符串值上使用Series.str....
函数,您没有完整的序列。