Shijo
Shijo

Reputation: 9711

Pandas - Convert String type to Float

I have the following column in a DF. How could I convert this column into a float(13,5) with 5 decimals.String length is always 18 characters. As a workaround , I have used string split function and joined return values.

df=pd.DataFrame(['+00000030454360000','-00000030734250000','-00000004643685000'],columns=['qty'])
print df.qty.str[:13]+'.'+df.qty.str[13:]

Expected Output

                    0
0  +304543.6
1  -307342.5
2  -46436.85

Upvotes: 2

Views: 806

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210842

Try this:

In [23]: pd.to_numeric(df.qty, errors='coerce') / 10**5
Out[23]:
0    304543.60
1   -307342.50
2    -46436.85
Name: qty, dtype: float64

Upvotes: 1

Related Questions