2016-10-17 5 views
4

für das folgende Array, mag ich Kommas mit Punkten ersetzen:ersetzen Komma mit Punkt Pandas

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) 

Ich habe verschiedene Wege versucht, aber ich kann leider nicht herausfinden, wie dies zu tun. Außerdem habe ich als Datenrahmen kann aber die Funktion nicht für:

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(',','.'))) 

Jeden Vorschlag? Dank

+1

'df.applymap (lambda x: ' 'str (x.replace (',',)))' funktioniert, ersetzt auf 'Punkt Komma pd .__ Version__ == ' 0.18.1'' – Zero

+0

Haben Sie das Ergebnis zurückgewiesen? 'df = df.applymap (Lambda x: str (x.replace (',', '.')))' – EdChum

+0

Auch wäre es für jede Spalte schneller: 'df = df.apply (Lambda x : x.str.replace (',', '.')) ' – EdChum

Antwort

3

Sie müssen das Ergebnis Ihrer betreiben zurück zuweisen als die Operation nicht Inplace ist, außer Sie apply oder stack und unstack mit vektorisiert str.replace, dies zu tun schneller nutzen können:

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 

die Schlüsselsache hier ist das Ergebnis zuweisen zurück:

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

Verwandte Themen