Lon_X Lat_Y
5,234234 6,3234234
5,234234 6,3234234
5,234234 6,3234234
5,234234 6,3234234
5,234234 6,3234234我有像上面那样的熊猫/数据中心的GPS坐标。但是,它们使用逗号分隔符。利用熊猫将这些转换成浮动GPS坐标的最佳方法是什么?
for item in frame.Lon_X:
float(item.replace(",", ".")) # makes the conversion but does not store it back我尝试过迭代项函数,但是看起来非常慢,并给出了一个警告,我不太明白:
for index, value in frame.Lon_X.iteritems():
frame.Lon_X[index] = float(value.replace(",", "."))请参阅文档中的注意事项:从ipykernel导入的http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy作为应用程序
发布于 2018-03-17 15:55:02
令人惊讶的是,在np系列上迭代比使用pd.series.str.replace更快。我用2m行系列做了下面的实验
setup = '''
import pandas as pd
import numpy as np
a = pd.Series(list('aabc') * 500000)
b = a.values.astype(str)
'''
a = '''
a[:] = a.str.replace("b", "d")
'''
b = '''
b[:] = np.char.replace(b, "b", "d")
'''
c = '''
for i, x in enumerate(b):
if "b" in x:
b[i] = "d"
'''
a_speed = min(timeit.Timer(a, setup=setup).repeat(7, 5))
b_speed = min(timeit.Timer(b, setup=setup).repeat(7, 5))
c_speed = min(timeit.Timer(c, setup=setup).repeat(7, 5))结果:
a_speed = 2.3304627019997497
b_speed = 6.832672896000076
c_speed = 1.9407824309996613
https://stackoverflow.com/questions/41403301
复制相似问题