我在當前是字符串的數據框中有一列。我需要將這些數據轉換爲浮點數並作爲數組提取,以便我可以使用座標對。試圖將一列字符串轉換爲浮點數
In [55]:apt_data['geotag']
Out[55]:
0 (40.7763, -73.9529)
1 (40.72785, -73.983307)
2 (40.7339, -74.0054)
3 (40.771731, -73.956313)
4 (40.8027, -73.949187)
Name: geotag, dtype: object'
首先我想:
apt_loc = apt_data['geotag']
apt_loc_ar = np.array(apt_loc['geotag'], dtype=dt)
但拋出此錯誤:
Traceback (most recent call last):
File "<ipython-input-60-3a853e355c9a>", line 1, in <module>
apt_loc_ar = np.array(apt_loc['geotag'], dtype=dt)
File "/python3.5/site-
packages/pandas/core/series.py", line 603, in __getitem__
result = self.index.get_value(self, key)
File "/python3.5/site-
packages/pandas/indexes/base.py", line 2169, in get_value
tz=getattr(series.dtype, 'tz', None))
File "pandas/index.pyx", line 98, in pandas.index.IndexEngine.get_value
(pandas/index.c:3557)
File "pandas/index.pyx", line 106, in pandas.index.IndexEngine.get_value
(pandas/index.c:3240)
File "pandas/index.pyx", line 156, in pandas.index.IndexEngine.get_loc
(pandas/index.c:4363)
KeyError: 'geotag'
我試着使用 apt_data['geotag'] = pd.to_numeric(apt_data['geotag'], errors='coerce')
這給了我NaN的所有條目。
謝謝。
所以每個觀測是在字符串內的元組? –
'type(apt_loc.get_value(0,'geotag')'產生'str'。我認爲括號和逗號也只是字符串值。 – epic556