首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Proximity散列类型错误,无法将系列转换为<class 'float'>

Proximity散列类型错误,无法将系列转换为<class 'float'>
EN

Stack Overflow用户
提问于 2022-09-01 10:29:14
回答 1查看 44关注 0票数 1
代码语言:javascript
复制
import proximityhash

# filtering the dataset with the required columns 
df_new=df.filter(['latitude', 'longitude','cell_radius'])

# assign the column  values to a variable
latitude = df_new['latitude']
longitude = df_new['longitude']
radius= df_new['cell_radius']
precision = 7

# passing the variable as the parameters to the proximityhash library 
# getting the values and assigning those to a new column as proximityhash
df_new['proximityhash']=df_new.apply([proximityhash.create_geohash(latitude,longitude,radius,precision=7)])
        
print(df_new) 

我使用了下面的代码,导入了一些数据集,使用该数据集,我尝试过滤必要的列,并将它们分配给3个变量(纬度、经度、半径),并尝试创建一个新列,作为“近似值散列”到新的dataframe,但它显示了如下错误:

在这里输入图像描述

代码语言:javascript
复制
 [1]: https://i.stack.imgur.com/2xW8S.png


TypeError                                 Traceback (most recent call last)
Input In [29], in <cell line: 15>()
     11 import pygeohash as gh
     13 import proximityhash
---> 15 df_new['proximityhash']=df_new.apply([proximityhash.create_geohash(latitude,longitude,radius,precision=7)])
     17 print(df_new)

File ~\Anaconda3\lib\site-packages\proximityhash.py:57, in create_geohash(latitude, longitude, radius, precision, georaptor_flag, minlevel, maxlevel)
     54 height = (grid_height[precision - 1])/2
     55 width = (grid_width[precision-1])/2
---> 57 lat_moves = int(math.ceil(radius / height)) #4
     58 lon_moves = int(math.ceil(radius / width)) #2
     60 for i in range(0, lat_moves):

File ~\Anaconda3\lib\site-packages\pandas\core\series.py:191, in _coerce_method.<locals>.wrapper(self)
    189 if len(self) == 1:
    190     return converter(self.iloc[0])
--> 191 raise TypeError(f"cannot convert the series to {converter}")

TypeError: cannot convert the series to <class 'float'>
EN

回答 1

Stack Overflow用户

发布于 2022-09-05 06:55:51

想出解决这个问题的方法,贴出答案,因为它可能对其他人有帮助。

定义函数并将库传递给该特定列。

代码语言:javascript
复制
# filtering the dataset with the required columns 
df_new=df[['latitude', 'longitude','cell_radius']]

# getting a specified row (Since running the whole process might kill the kernel)
df_new = df_new.iloc[:100, ]

# predefined the precision value.
precision = 7

def PH(row):
    latitude = row['latitude']
    longitude = row['longitude']
    cell_radius = row['cell_radius']
    row['proximityhash'] = [proximityhash.create_geohash(float(latitude),float(longitude),float(cell_radius),precision=7)]
    return row

df_new = df_new.apply(PH, axis=1)  

df_new['proximityhash'] =pd.Series(df_new['proximityhash'], dtype="string")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73567885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档