1
import pandas as pd    
df = pd.DataFrame([["one", 1.2, "", "4,3"],
   ["two","1,7", "2,4", 0.55],
   ["three","", 5.4, "3,9"]], 
   columns=['a','b','c','d'])

How can I replace the commas in the columns b to d to points and make these columns numeric? Column a (or any number of leading columns) should be retained untouched (can contain commas as well)

ronnydw
  • 923
  • 16
  • 24

2 Answers2

5
import re    

for col in ['b', 'c', 'd']:
    df[col] = pd.to_numeric(df[col].apply(lambda x: re.sub(',', '.', str(x))))
J_Heads
  • 490
  • 2
  • 11
1

Just an update on this topic:

pandas now added a replace method, so we can avoid the use of lambda.

df.replace(to_replace=",", value=".", regex=True)
Marromenu
  • 23
  • 6