Search code examples
pythonpython-3.xpandasencodingone-hot-encoding

how to convert object Dtype to int64?


I've the below data.

enter image description here

When I checked the DType of these fields it is showing as object, now my requirement is I would like to convert them into int64

#   Column        Non-Null Count  Dtype  
---  ------        --------------  -----  
 0   area_type     3 non-null      object 
 1   availability  3 non-null      object 
 2   location      3 non-null      object 
 3   size          3 non-null      object 
 4   society       3 non-null      object 

Can someone help me with the code to convert them. I tried using the below code but it throwed me an error.

dataset['area_type'] = dataset['area_type'].str.replace(',','').astype(int)

ERROR

ValueError: invalid literal for int() with base 10: 'Super built-up  Area'

Solution

  • I've tried using the LabelEncoder and is working fine for me.

    from sklearn.preprocessing import LabelEncoder 
      
    le = LabelEncoder() 
      
    dataset['area_type']= le.fit_transform(dataset['area_type']) 
    dataset['availability']= le.fit_transform(dataset['availability'])
    dataset['location']= le.fit_transform(dataset['location'])
    dataset['size']= le.fit_transform(dataset['size'])
    dataset['society']= le.fit_transform(dataset['society'])