简体   繁体   中英

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.

数据帧头

数据帧信息

You can try by doing df["Bare Nuclei"].astype(np.int64) but as far as I can see the problem is something else. Pandas first reads all the data to best estimate the data type for each column, then only makes the data frame. So, there must be some entries in the data frame which are not integer types, ie, they may contain some letters. In that case, also typecasting should give an error. So you need to remove those entries before successfully making the table 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? 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

you can fix this at import using

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 = l1.dropna()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM