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

如何将DataFrame中的经纬度坐标字符串拆分为纬度、经度两列?

你报错的核心原因是Location_1列存储的是字符串格式的坐标,而非元组类型,因此无法直接用[:, 0]的切片方式提取纬度和经度。

你可以通过字符串清洗、拆分后转浮点类型的方式完成提取,以下是可直接运行的代码:

# 清洗坐标字符串,拆分后直接生成纬度、经度两列
df[['lat', 'lon']] = df['Location_1'].str.strip('()')\
                                    .str.split(', ', expand=True)\
                                    .astype(float)

上述代码逻辑:先通过str.strip('()')去除每个坐标字符串首尾的括号,再按, (逗号加空格)拆分出两个坐标值,expand=True参数会将拆分结果直接返回为两列DataFrame,最后统一转为浮点类型赋值给新列即可。

如果数据集存在格式不规范的脏数据,可以用以下写法兼容,非法格式的坐标会被转为NaN避免报错:

import pandas as pd

# 拆分坐标字符串
coord_split = df['Location_1'].str.strip('()').str.split(', ', expand=True)
# 转浮点,非法值转为NaN
df['lat'] = pd.to_numeric(coord_split[0], errors='coerce')
df['lon'] = pd.to_numeric(coord_split[1], errors='coerce')

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 16:45:04