df[["Lon_X", "Lat_Y"]] = df[["Lon_X", "Lat_Y"]].applymap(lambda x: float(x.replace(",", ".")))
df
以下是关于这些替代方法的一些基准,to_float_inplace比所有其他方法都快得多:
数据:
^{pr2}$
# 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