0

I extracted some data from investing but columns values are all dtype = object, so i cant work with them... how should i convert object to float?

(2558 6.678,08 2557 6.897,23 2556 7.095,95 2555 7.151,21 2554 7.093,34 ... 4 4.050,38 3 4.042,63 2 4.181,13 1 4.219,56 0 4.223,33 Name: Alta, Length: 2559, dtype: object)

What i want is : 2558 6678.08 2557 6897.23 2556 7095.95 2555 7151.21 2554 7093.34 ... 4 4050.38 3 4042.63 2 4181.13 1 4219.56 0 4223.33 Name: Alta, Length: 2559, dtype: float

Tried to use the a function which would replace , for .

def clean(x): x = x.replace(".", "").replace(",",".")

but it doesnt work cause dtype is object

Thanks!

2
  • can you clear what is your input and what output you expect ? Commented May 24, 2020 at 19:23
  • i already edited @ManishKumarSingh Commented May 24, 2020 at 19:33

4 Answers 4

0

If your local locale uses commas as decimal signs, you can use locale.atof (if not you'll first have to set an appropriate locale):

>>> s = pd.Series(['6.678,08','6.897,23'], name='Alta')
>>> s
0    6.678,08
1    6.897,23
Name: Alta, dtype: object
>>> import locale
>>> locale.setlocale(locale.LC_NUMERIC, '')
'de_DE.UTF-8'
>>> s.apply(locale.atof)
0    6678.08
1    6897.23
Name: Alta, dtype: float64
Sign up to request clarification or add additional context in comments.

1 Comment

Ty so much! it really helped me!
0

refer to: How to convert datatype:object to float64 in python?

for i in range(0, len(df.columns)):
    df.iloc[:,i] = pd.to_numeric(df.iloc[:,i], errors='ignore')
    # errors='ignore' lets strings remain as 'non-null objects'

this can convert all numerical values to float64 or int64.

Comments

0

To replace your values, try this. result is your dataframe, col is your column name

result[col] = result[col].apply(lambda x: x.str.replace(".","").str.replace(",","."))

If you need to convert this variable to a float, try this

result[col] = result[col].astype(float)

1 Comment

Thank u but it didnt work...it keeps appearing ValueError: could not convert string to float: '6.678,08'...
0

That is because there is a comma between the value Because a float cannot have a comma, you need to first replace the comma and then convert it into float result[col] = result[col].str.replace(",","").astype(float)

1 Comment

Changing the locale seems to be a more useful solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.