-2

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'
Nirali Khoda
  • 1,064
  • 5
  • 19
Vikas
  • 189
  • 5
  • What is your desired output? Because you cannot convert a string into a int if it's not a number string... – Let's try Oct 03 '20 at 09:54
  • I'm planning to perform **Linear Regression**, so in order to do that I've to convert them into integer. – Vikas Oct 03 '20 at 09:59
  • Then you should use either [one-hot-encoding](https://stackoverflow.com/questions/37292872/how-can-i-one-hot-encode-in-python) or something like [this](https://stackoverflow.com/questions/32011359/convert-categorical-data-in-pandas-dataframe), not the function `int()`... – Let's try Oct 03 '20 at 10:02
  • Could you please Upload Data? and desired Output ? – Nirali Khoda Oct 06 '20 at 07:06

1 Answers1

0

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'])
Vikas
  • 189
  • 5