简体   繁体   English

如何在vandas数据帧中将vincenty距离转换为float

[英]How do I convert vincenty distance to float in pandas dataframe

I have problem to analyse vincenty distance because the format is object and have km metrics in there, I want to analyse further. 我有分析vincenty距离的问题,因为格式是object并且在那里有km度量,我想进一步分析。 I want to convert vincenty distance to float format 我想将vincenty距离转换为float格式

Here's the data 这是数据

customer_id lat_free    long_free   lat_device  long_device radius      timestamp
7509        -6.283468   106.857636  -7.802388   110.368660  1264.000000 2017-12-14 21:18:40.327
7509        -6.283468   106.857636  -7.804296   110.367192  14.000000   2017-12-15 20:02:21.923

Here's my code 这是我的代码

from geopy.distance import vincenty
df['Vincenty_distance'] = df.apply(lambda x: vincenty((x['lat_free'], x['long_free']), (x['lat_device'], x['long_device'])), axis = 1)

This is the result 这是结果

customer_id lat_free    long_free   lat_device  long_device radius      timestamp                 Vincenty_distance
7509        -6.283468   106.857636  -7.802388   110.368660  1264.000000 2017-12-14 21:18:40.327   422.7123873310482 km
7509        -6.283468   106.857636  -7.804296   110.367192  14.000000   2017-12-15 20:02:21.923   422.64674499172787 km

I need to convert Vincenty_distance to float 我需要将Vincenty_distance转换为float

The best is add .km : 最好的是添加.km

df['Vincenty_distance'] = df.apply(lambda x: vincenty((x['lat_free'], x['long_free']), (x['lat_device'], x['long_device'])).km, axis = 1)

Or use after processing - convert to string , remove last letters and cast to float s: 或者在处理后使用 - 转换为string ,删除最后一个字母并转换为float

df['Vincenty_distance'] = df['Vincenty_distance'].astype(str).str[:-3].astype(float)

print (df)
   customer_id  lat_free   long_free  lat_device  long_device  radius  \
0         7509 -6.283468  106.857636   -7.802388   110.368660  1264.0   
1         7509 -6.283468  106.857636   -7.804296   110.367192    14.0   

                 timestamp  Vincenty_distance  
0  2017-12-14 21:18:40.327         422.712361  
1  2017-12-15 20:02:21.923         422.646709  

print (df.dtypes)

customer_id            int64
lat_free             float64
long_free            float64
lat_device           float64
long_device          float64
radius               float64
timestamp             object
Vincenty_distance    float64
dtype: object

You can use str.replace to remove "km" and use apply to set float to the series. 您可以使用str.replace删除“km”并使用apply将float设置为系列。

Ex: 例如:

df["Vincenty_distance"] = df["Vincenty_distance"].str.replace(" km", "").apply(float)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Pandas Dataframe 运行回归时,如何修复“ValueError:无法将字符串转换为浮点数”? - How do I fix “ValueError: could not convert string to float” when running regression from Pandas Dataframe? 如何将 Pandas DataFrame 中的 integer 'category' dtypes 转换为 'int64'/'float64'? - How do I convert integer 'category' dtypes in a Pandas DataFrame to 'int64'/'float64'? 如何将pandas DataFrame中的列从str(科学计数法)转换为numpy.float64? - How do I convert a column from a pandas DataFrame from str (scientific notation) to numpy.float64? 我如何将这个 pandas dataframe 转换成记录? - How do I convert this pandas dataframe into records? 如何在Pandas Dataframe中将比率转换为浮点数? - How to convert ratio to float in Pandas Dataframe? 如何将str转换为pandas dataframe中的float - how to convert str to float in pandas dataframe 如何将整个数据框值转换为浮于熊猫 - how to convert entire dataframe values to float in pandas 如何将 pandas dataframe 中的字符串转换为浮点数或整数 - How to convert a string in pandas dataframe into float or int 如何将数据框中的符号转换为 python 中的浮点数? - How do I convert a symbol within a dataframe to a float in python? 在pandas中,如何将一系列float或none转换为带整数的字符串 - In pandas, how do I convert a series of float or none to strings with integers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM