简体   繁体   English

python中object数据类型如何转换成int64?

[英]How to convert object data type into int64 in python?

I have a dataset and it has one variable as object data type, i have to convert it to int64 type.我有一个数据集,它有一个数据类型为 object 的变量,我必须将它转换为 int64 类型。

数据帧头

数据帧信息

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.

相关问题 如何将数据帧对象转换为 Int64? - How to convert dataframe object into Int64? 如何将对象 Dtype 转换为 int64? - how to convert object Dtype to int64? 如何总结一个int64和对象类型? - How to sum a int64 and object type? 如何在更新 Pandas 后将 object 数据类型转换为 Float64 和 Int64 以用于 NAN 值的插值 - How to convert object data type to Float64 and Int64 after updating the Pandas for interpolation for NAN values 将数据类型为 Int64 的列转换为<na>值到 object 与 nan 值</na> - Convert a column of data type Int64 with <NA> values to object with nan values Python - 类型错误:“int64”类型的 Object 不是 JSON 可序列化的 - Python - TypeError: Object of type 'int64' is not JSON serializable Python 错误:TypeErrpr:int64 类型的对象不是 JSON 可序列化的 - Python error: TypeErrpr: Object of type int64 is not JSON serializable Python-插入数据时发生错误(TypeError:“ int64”类型的对象不可JSON序列化) - Python - Error when inserting data (TypeError: Object of type 'int64' is not JSON serializable) 如何在Python中将具有对象/类别数据类型的多列熊猫数据框转换为int64? - how to convert multiple columns of pandas dataframe with object/categorical datatype to int64 in python? 如何将 int64 转换为分钟? 蟒蛇熊猫 - How convert an int64 to minutes ? Python Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM