【发布时间】:2016-09-08 17:08:18
【问题描述】:
我确信这是一件简单的事情,但我是 Python 新手,无法解决!
我有一个数据框,其中一列包含坐标,我想删除括号并将纬度/经度值添加到单独的列中。
当前数据框:
gridReference
(56.37769816725615, -4.325049868061924)
(56.37769816725615, -4.325049868061924)
(51.749167440074324, -4.963575226888083)
想要的数据框:
Latitude Longitude
56.37769816725615 -4.325049868061924
56.37769816725615 -4.325049868061924
51.749167440074324 -4.963575226888083
感谢您的帮助
编辑: 我试过了:
df['lat'], df['lon'] = df.gridReference.str.strip(')').str.strip('(').str.split(', ').values.tolist()
但我得到了错误:
AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas
然后我尝试添加:
df['gridReference'] = df['gridReference'].astype('str')
并得到错误:
ValueError: too many values to unpack (expected 2)
任何帮助将不胜感激,因为我不知道如何使这项工作! :)
编辑:
我不断收到错误
AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas
df.dtypes 的输出是:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 22899 entries, 0 to 22898
Data columns (total 1 columns):
LatLon 22899 non-null object
dtypes: object(1)
df.info() 的输出是:
gridReference object
dtype: object
【问题讨论】:
-
请提供您的数据样本以及您的尝试。
-
我添加了一些信息。如果您能以任何方式提供帮助,我们将不胜感激!