1

I am trying to convert a value of a column in the dataframe. column name is size. it has data as 11.1 K or 51.6M, i.e ending in K or M and has object data type. i want to write an apply function which converts this value to 11.1 if it is ending in K and 516000 if it is ending in M . Any help?

I am trying to code for this in python 3

  • Solved here, https://stackoverflow.com/questions/39684548/convert-the-string-2-90k-to-2900-or-5-2m-to-5200000-in-pandas-dataframe – S. Chirag Dec 17 '20 at 09:27

1 Answers1

0

Lots of way to do this, a simple way would be to use pd.eval with replace

df = pd.DataFrame({'A' : ['56.1M', '11.1K']})

print(df)

   A
0  56.1M
1  11.1K

df['B'] = df['A'].replace({'M' : '*10000', 'K' : '*1'},regex=True).map(pd.eval)

print(df)

       A         B
0  56.1M  561000.0
1  11.1K      11.1
Umar.H
  • 22,559
  • 7
  • 39
  • 74