替换数据帧中的值Python
问题描述:
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坐标的最佳方法是什么?替换数据帧中的值Python
for item in frame.Lon_X:
float(item.replace(",", ".")) # makes the conversion but does not store it back
我已经试过iteritems功能,但似乎很慢,给我一个警告,我真的不明白:
for index, value in frame.Lon_X.iteritems():
frame.Lon_X[index] = float(value.replace(",", "."))
见警告文档中: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy 从ipykernel进口kernelapp为app
答
您可以使用applymap:
df[["Lon_X", "Lat_Y"]] = df[["Lon_X", "Lat_Y"]].applymap(lambda x: float(x.replace(",", ".")))
df
以下是有关这些替代一些基准测试软件,to_float_inplace
是显著比所有其他方法更快:
数据:
df = pd.DataFrame({"Lon_X": ["5,234234" for i in range(1000000)], "Lat_Y": ["6,3234234" for i in range(1000000)]})
# to_float_inplace
def to_float_inplace(x):
x[:] = x.str.replace(',', '.').astype(float)
%timeit df.apply(to_float_inplace)
# 1 loops, best of 3: 269 ms per loop
# applymap + astype
%timeit df.applymap(lambda x: x.replace(",", ".")).astype(float)
# 1 loops, best of 3: 1.26 s per loop
# to_float
def to_float(x):
return x.str.replace(',', '.').astype(float)
%timeit df.apply(to_float)
# 1 loops, best of 3: 1.47 s per loop
# applymap + float
%timeit df.applymap(lambda x: float(x.replace(",", ".")))
# 1 loops, best of 3: 1.75 s per loop
# replace with regex
%timeit df.replace(',', '.', regex=True).astype(float)
# 1 loops, best of 3: 1.79 s per loop
答
试试这个:
df.applymap(lambda x: float(x.replace(",", ".")))
编辑:忘了map
作为@Psidom显示
答
您可以将熊猫的量化方法一起就地一轴:
def to_float_inplace(x):
x[:] = x.str.replace(',', '.').astype(float)
df.apply(to_float_inplace)
答
你可以跳过使用申请,并直接与更换replace
方法regex=True
df.replace(',', '.', regex=True).astype(float)