how to convert comma seperated values to integer in pandas

I have following dataframe in pandas.

  order_id    no
  1           1,234,450,445.00 
  2           1,234,450,446.00
  3           1,234,450,447.00

I want to convert the no column to integer. Following is my desired dataframe.

  order_id    no
  1           1234450445 
  2           1234450446
  3           1234450447

When I do dtypes, it shows as float64

I tried following

df['no'] = (pd.to_numeric(df['no'].str.replace(',',''), errors='coerce'))

How can I convert this to integer in pandas?

2 answers

  • answered 2020-07-05 05:00 wa007

    Hope can help you.

    import pandas as pd
    import numpy as np
    df['no'] = df['no'].astype(str).apply(lambda x: "".join(x.split(","))).astype(np.float64).astype(np.int64)
    

    or

    df['new_no'] = df['no'].astype(str).apply(lambda x: int(float("".join(x.split(",")))))
    

  • answered 2020-07-05 05:04 Scott Boston

    Here is a way, go through float type first:

    df['no'].str.replace(',','').astype(float).astype(int)
    

    Output:

    0    1234450445
    1    1234450446
    2    1234450447
    Name: no, dtype: int64
    

    Or slice '.00' off then end of all rows:

    df['no'].str.strip('.00').str.replace(',','').astype(int)