Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace comma with dot Pandas

Tags:

python

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?

like image 678
Juliana Rivera Avatar asked Oct 17 '16 09:10

Juliana Rivera


People also ask

How do I change a comma to a dot in Python?

str. replace(',', '. ') replaces the comma for a dot.

How do I change a DataFrame value in Python?

Pandas DataFrame replace() MethodThe replace() method replaces the specified value with another specified value. The replace() method searches the entire DataFrame and replaces every case of the specified value.

How do pandas change objects to float?

pandas Convert String to FloatUse pandas DataFrame. astype() function to convert column from string/int to float, you can apply this on a specific column or on an entire DataFrame. To cast the data type to 54-bit signed float, you can use numpy. float64 , numpy.

How do I replace values in a pandas Dataframe?

In this post, you learned how to use the Pandas replace method to, well, replace values in a Pandas dataframe. The .replace () method is extremely powerful and lets you replace values across a single column, multiple columns, and an entire dataframe. The method also incorporates regular expressions to make complex replacements easier.

How to replace a comma with a dot in a column?

If you need to replace commas with dots in particular columns, try data ["column_name"]=data ["column_name"].str.replace (',','.') to avoid 'str' object has no attribute 'str' error. If you are reading in with read_csv, you can specify how it interprets decimals with the decimal parameter.

How to replace the methods in strings with symbols in Python?

This is more of a logical approach in which we swap the symbols considering third variables. The replace method can also be used to replace the methods in strings. We can convert “, ” to a symbol then convert “.” to “, ” and the symbol to “.”. For more reference visit Python String Methods .

How do you convert a string to a symbol in Python?

We can convert “, ” to a symbol then convert “.” to “, ” and the symbol to “.”. For more reference visit Python String Methods . str1 = str1.replace ('third', '.')


Video Answer


1 Answers

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

like image 94
EdChum Avatar answered Sep 27 '22 19:09

EdChum