Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how do you convert data frame column values to integer

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading