pythonpandasint64

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.

dataframe head

dataframe info


Solution

  • 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, i.e., 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.