gpt4 book ai didi

python - 试图将一列字符串转换为 float

转载 作者:太空宇宙 更新时间:2023-11-04 00:35:17 25 4
gpt4 key购买 nike

我在当前为字符串的数据框中有一列。我需要将此数据转换为 float 并提取为数组,以便我可以使用坐标对。

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。

谢谢。

最佳答案

您可以使用 ast 模块中的 literal_eval 并将函数应用于您的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com