andre.fonta90
andre.fonta90

Reputation: 35

How do I convert DataFrame column of type "string" to "float" using .replace?

In my DataFrame, the "Value_String" column consists of strings that are either:

Therefore, I tried to create a new column and convert the string to float with the following lambda function:

to_replace = '$,'

df['Value_Float'] = df[df['Value_String'].apply(lambda x: 0 if x == 'None' 
else float(x.replace(y, '')) for y in to_replace)]

This actually generates a "TypeError: 'generator' object is not callable".

How can I solve this?

Upvotes: 0

Views: 169

Answers (1)

Chris
Chris

Reputation: 16147

The numpy where method is very helpful for conditionally updating values. In this case where the value is not 'None' we will use the replace function. However since str.replace uses regex by default, we need to change the pattern to a literal dollar sign OR a comma

import pandas as pd
import numpy as np
df = pd.DataFrame({'Value_String':["$1,000","None"]})
df['Value_String'] = np.where(df['Value_String']!='None', df['Value_String'].str.replace('\$|,',''), df['Value_String'])
print(df)

Output

  Value_String
0         1000
1         None

Upvotes: 1

Related Questions