我在当前为字符串的数据框中有一列。我需要将这些数据转换为浮点数并提取为数组,以便可以使用坐标对。
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。
谢谢。
最佳答案
您可以使用literal_eval
模块中的ast
并将一个函数应用于DataFrame,如下所示:
import pandas as pd
from ast import literal_eval as le
df = pd.DataFrame(["(40.7763, -73.9529)","(40.72785, -73.983307)"], columns=["geotag"])
df["geotag"] = df["geotag"].apply(func=lambda x: le(x))
输出:
>>> for k in df["geotag"]:
for j in k: print(type(j))
<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>
关于python - 尝试将一列字符串转换为float,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44316980/