I have a dataframe df
ID | a | b | c |
---|---|---|---|
1 | . | 24,000.8 | 400.9 |
2 | 5,000.9 | 79,000.8 | . |
4 | 6,650.7 | . | 345.9 |
How do I convert these columns (a,b,c,...) from a string to a float (i.e. changing '.' to 0 and removing the commas in order to convert from string to float)?
def string2Float(df,columnName):
if df[columnName] == '.':
df[columnName] = 0
else:
df[columnName] = float(df[columnName].replace(',',''))
return df[columnName]
Is there a way to apply this function to the dataframe? I only want to apply it to column a,b, and c. In the future I'll want to apply it to column a,b, and c etc in df1, df2, df3 etc.
For example this works for column a in df1:
def string2Float(dfRow):
if dfRow == '.':
x = 0
else:
x = float(dfRow.replace(',',''))
return x
convertCols = ['a','b','c']
for col in convertCols:
df1[col] = df1.apply(lambda row: string2Float(row[col]),axis=1)
How can i apply it to multiple dataframes?