david
david

Reputation: 11

pandas string to float

I have an array.

array(['+$35.00', '="-$5.00"', '+$42.24', '+$10.56', '="-$20.00"',`
       '+$60.00', '="-$10.00"', '+$21.00', '+$18.50', '+$10.00',
       '+$19.00', '+$34.40', 0], dtype=object)

I know this has been covered many times but I cant seem to convert column in pandas to a float.

Upvotes: 1

Views: 44

Answers (1)

mozway
mozway

Reputation: 260300

Assuming a the array, you can use:

# convert the array to Series
s = pd.Series(a)
# extract float representation from strings, convert to numeric
# fill with values that were already floats
s = pd.to_numeric(s.str.extract(r'(\d+(?:\.\d+)?)', expand=False)).fillna(s)

output:

0     35.00
1      5.00
2     42.24
3     10.56
4     20.00
5     60.00
6     10.00
7     21.00
8     18.50
9     10.00
10    19.00
11    34.40
12     0.00
dtype: float64

Upvotes: 1

Related Questions