Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

python - Replace comma with dot 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)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...