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!

zoramind
  • 25
  • 1
  • 5

4 Answers4

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.

VR7
  • 112
  • 3
  • 16
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)
Chris R
  • 13
  • 5
  • Thank u but it didnt work...it keeps appearing ValueError: could not convert string to float: '6.678,08'... – zoramind May 24 '20 at 19:44
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
Stef
  • 28,728
  • 2
  • 24
  • 52
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)