用点 pandas 替换逗号 [英] Replace comma with dot Pandas

查看:46
本文介绍了用点 pandas 替换逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于以下数组,我想用点替换逗号:

Given the following array, I want to replace commas with dots:

array(['0,140711', '0,140711', '0,0999', '0,0999', '0,001', '0,001',
       '0,140711', '0,140711', '0,140711', '0,140711', '0,140711',
       '0,140711', 0L, 0L, 0L, 0L, '0,140711', '0,140711', '0,140711',
       '0,140711', '0,140711', '0,1125688', '0,140711', '0,1125688',
       '0,140711', '0,1125688', '0,140711', '0,1125688', '0,140711',
       '0,140711', '0,140711', '0,140711', '0,140711', '0,140711',
       '0,140711', '0,140711', '0,140711', '0,140711', '0,140711',
       '0,140711', '0,140711', '0,140711', '0,140711', '0,140711',
       '0,140711', '0,140711', '0,140711', '0,140711'], dtype=object)

我一直在尝试不同的方法,但是我不知道该怎么做. 另外,我已将其导入为pandas DataFrame,但无法应用该功能:

I've been trying different ways but I can't figure out how to do this. Also, I have imported it as a pandas DataFrame but can't apply the function:

df
      1-8        1-7
H0   0,140711   0,140711
H1     0,0999     0,0999
H2      0,001      0,001
H3   0,140711   0,140711
H4   0,140711   0,140711
H5   0,140711   0,140711
H6          0          0
H7          0          0
H8   0,140711   0,140711
H9   0,140711   0,140711
H10  0,140711  0,1125688
H11  0,140711  0,1125688
H12  0,140711  0,1125688
H13  0,140711  0,1125688
H14  0,140711   0,140711
H15  0,140711   0,140711
H16  0,140711   0,140711
H17  0,140711   0,140711
H18  0,140711   0,140711
H19  0,140711   0,140711
H20  0,140711   0,140711
H21  0,140711   0,140711
H22  0,140711   0,140711
H23  0,140711   0,140711 

df.applymap(lambda x: str(x.replace(',','.')))

任何建议如何解决这个问题?

Any suggestions how to solve this?

推荐答案

由于操作不适当,您需要分配操作结果,此外您还可以使用

You need to assign the result of your operate back as the operation isn't inplace, besides you can use apply or stack and unstack with vectorised str.replace to do this quicker:

In [5]:
df.apply(lambda x: x.str.replace(',','.'))

Out[5]:
          1-8        1-7
H0   0.140711   0.140711
H1     0.0999     0.0999
H2      0.001      0.001
H3   0.140711   0.140711
H4   0.140711   0.140711
H5   0.140711   0.140711
H6          0          0
H7          0          0
H8   0.140711   0.140711
H9   0.140711   0.140711
H10  0.140711  0.1125688
H11  0.140711  0.1125688
H12  0.140711  0.1125688
H13  0.140711  0.1125688
H14  0.140711   0.140711
H15  0.140711   0.140711
H16  0.140711   0.140711
H17  0.140711   0.140711
H18  0.140711   0.140711
H19  0.140711   0.140711
H20  0.140711   0.140711
H21  0.140711   0.140711
H22  0.140711   0.140711
H23  0.140711   0.140711

In [4]:    
df.stack().str.replace(',','.').unstack()

Out[4]:
          1-8        1-7
H0   0.140711   0.140711
H1     0.0999     0.0999
H2      0.001      0.001
H3   0.140711   0.140711
H4   0.140711   0.140711
H5   0.140711   0.140711
H6          0          0
H7          0          0
H8   0.140711   0.140711
H9   0.140711   0.140711
H10  0.140711  0.1125688
H11  0.140711  0.1125688
H12  0.140711  0.1125688
H13  0.140711  0.1125688
H14  0.140711   0.140711
H15  0.140711   0.140711
H16  0.140711   0.140711
H17  0.140711   0.140711
H18  0.140711   0.140711
H19  0.140711   0.140711
H20  0.140711   0.140711
H21  0.140711   0.140711
H22  0.140711   0.140711
H23  0.140711   0.140711

这里的关键是分配结果:

the key thing here is to assign back the result:

df = df.stack().str.replace(',','.').unstack()

这篇关于用点 pandas 替换逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆