0

I have a pandas column with str values. I need to change its type to int. The problem is that all values are separated with (' ') to differentiate the Ks and the Ms, ex:

a = '17 000 000'
int(a)
print(a)

output:

17000000
Bill Huang
  • 4,491
  • 2
  • 13
  • 31

1 Answers1

0

It is also possible to do this by changing locale settings and applying locale.atof().

CAUTION: don't use this if other locale-related logics were present.

Code:

import locale
import pandas as pd

# data
df = pd.DataFrame({"A":["17 000 000", "15 000,22"]})

# change locale settings    
locale._override_localeconv["thousands_sep"] = " "
locale._override_localeconv["decimal_point"] = ","

# apply
df["A"] = df["A"].apply(locale.atof)

Result:

print(df)
             A
0  17000000.00
1     15000.22

Personally I think the df["A"].str.replace(" ", "").astype(int) construct mentioned by @Erfan is recommended.

Bill Huang
  • 4,491
  • 2
  • 13
  • 31