如何将对象 Dtype 转换为 int64?
how to convert object Dtype to int64?
我有以下数据。
当我检查这些字段的 DType 时,它显示为 object,现在我的要求是我想将它们转换为 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
有人可以帮我转换代码吗?我尝试使用下面的代码,但它抛出了一个错误。
dataset['area_type'] = dataset['area_type'].str.replace(',','').astype(int)
错误
ValueError: invalid literal for int() with base 10: 'Super built-up Area'
我试过使用 LabelEncoder,对我来说效果很好。
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'])
我有以下数据。
当我检查这些字段的 DType 时,它显示为 object,现在我的要求是我想将它们转换为 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
有人可以帮我转换代码吗?我尝试使用下面的代码,但它抛出了一个错误。
dataset['area_type'] = dataset['area_type'].str.replace(',','').astype(int)
错误
ValueError: invalid literal for int() with base 10: 'Super built-up Area'
我试过使用 LabelEncoder,对我来说效果很好。
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'])