[英]How to convert object data type into int64 in python?
You can try by doing df["Bare Nuclei"].astype(np.int64)
but as far as I can see the problem is something else.您可以尝试
df["Bare Nuclei"].astype(np.int64)
但据我所知,问题出在其他地方。 Pandas first reads all the data to best estimate the data type for each column, then only makes the data frame. Pandas首先读取所有数据以最好地估计每一列的数据类型,然后只制作数据框。 So, there must be some entries in the data frame which are not integer types, ie, they may contain some letters.
因此,数据框中一定有一些不是 integer 类型的条目,即它们可能包含一些字母。 In that case, also typecasting should give an error.
在那种情况下,类型转换也应该给出错误。 So you need to remove those entries before successfully making the table integer.
因此,您需要在成功创建表 integer 之前删除这些条目。
ive the same problem with the same dataset我对同一个数据集有同样的问题
there are lots of "?"有很多“?” in the data for the 'bare_nuclei' column (16) of them in the csv itself you need to use the error handling to drop the rows with the?
在 csv 本身的“bare_nuclei”列 (16) 的数据中,您需要使用错误处理来删除行? in the bare_nuclei column, aslo as a heads up dont name 'class' column class as thats a reserved keyword in python and thats also going to cause problems later
在 bare_nuclei 列中,请注意不要将“类”列命名为 class,因为这是 python 中的保留关键字,这也会在以后引起问题
you can fix this at import using您可以在导入时使用修复此问题
missing_values = ["NA","N/a",np.nan,"?"] missing_values = ["NA","N/a",np.nan,"?"]
l1 = pd.read_csv("../DataSets/Breast cancer dataset/breast-cancer-wisconsin.data",header=None,na_values=missing_values, names=['id','clump_thickness','uniformity_of_cell_size','uniformity_of_cell_shape','marginal_adhesion','single_epithelial_cell_size','bare_nuclei','bland_chromatin','normal_nucleoli','mitoses','diagnosis']) l1 = pd.read_csv("../DataSets/Breast cancer dataset/breast-cancer-wisconsin.data",header=None,na_values=missing_values,names=['id','clump_thickness','uniformity_of_cell_size','uniformity_of_cell_shape ','marginal_adhesion','single_epithelial_cell_size','bare_nuclei','bland_chromatin','normal_nucleoli','有丝分裂','诊断'])
l1 = l1.dropna() l1 = l1.dropna()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.