You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何对pandas dataframe列文本切片提取城市名并新增City列

解决方法

现有Purchase Address字段的格式为「街道地址, 城市名, 州+邮编」,各部分通过, (逗号加空格)分隔,你可以先对字段按分隔符做拆分,再取拆分结果的第2个元素(索引从0开始计数,对应索引为1)作为城市值。
补全后的代码如下:

all_data['City'] = all_data['Purchase Address'].str.split(', ').str[1]

如果要兼容城市名前后可能存在的多余空格,可增加去空格处理:

all_data['City'] = all_data['Purchase Address'].str.split(', ').str[1].str.strip()

内容的提问来源于stack exchange,提问作者Seshu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 14:36:03