1

I have a data frame in the following manner

 d=pd.DataFrame({"col1":['a','b','c','d'],
                "col2":['1.2M', '13.22B' , '0.2M' , '155.6M']})

I want to convert 'col2' in integer format

expected Output

d=pd.DataFrame({"col1":['a','b','c','d'],
                "col2":['1200000', '13220000000' , '200000' , '155600000']})
Aviv Yaniv
  • 6,188
  • 3
  • 7
  • 22
Amit
  • 763
  • 1
  • 5
  • 14

1 Answers1

2

Use apply to apply this lambda expression which translate and multiply according to the suffix:

import pandas as pd

df = pd.DataFrame({"col1": ["a", "b", "c", "d"],
                  "col2": ['1.2M', '13.22B', '0.2M', '155.6M']})

tens = dict(k=10e3, m=10e6, b=10e9)

df['col2'] = df['col2'].apply(lambda x : int(float(x[0:-1]) * tens[x[-1].lower()]))

print(df)

Output:

  col1          col2
0    a      12000000
1    b  132200000000
2    c       2000000
3    d    1556000000
Aviv Yaniv
  • 6,188
  • 3
  • 7
  • 22