I need to convert data frame column to int.
df['Slot'].unique()
displays this:
array(['1', '2', '3', '4', 1, 3, 5], dtype=object)
some values have ” around it some dont.
I tried to convert the data type to int as below:
df['Slot']=df['Slot'].astype('Int64')
I get this error:
TypeError: cannot safely cast non-equivalent object to int64
Any ideas how to convert to int?
>Solution :
You should first convert to numeric, then cast to Int64:
df['Slot'] = pd.to_numeric(df['Slot']).astype('Int64')
After casting the type:
df.dtypes
Slot Int64
dtype: object
As noted in comments, if you use numpy’s int64 type this works from scratch:
df['Slot'].astype('int64')