python dataframe 替换,替换数据框Python中的值 (Replace values in dataframe Python)

You can use applymap:

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

df

vJpgL.jpg

Here is some benchmark about these alternatives, to_float_inplace is significantly faster than all other methods:

Data:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值