pythondataframe如何替换值_替换dataframe Python中的值

df[["Lon_X", "Lat_Y"]] = df[["Lon_X", "Lat_Y"]].applymap(lambda x: float(x.replace(",", ".")))

df

vJpgL.jpg

以下是关于这些替代方法的一些基准,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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值