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 to stop pandas from inferring string value "Infinity" as inf and changing datatype to float64

I have a dataset with a column "Vendor" and it has multiple values as "Infinity", when I use read_csv, pandas automatically converted the Vendor column values to inf and changed its datatype to float64.

This is the test dataset I am using

Vendor, Value
Infinity,1
Infinity,2
Infinity,3
Infinity,4
Infinity,5

This is the result I am getting with inf value and datatype as float64

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

Vendor  Value
inf     1
inf     2
inf     3
inf     4
inf     5
Name: Vendor, dtype: float64

Is there a way to avoid default infering of Infinity to inf?

>Solution :

You can pass the dtype argument to set explicit column types for particular column names, like so:

pd.read_csv(file_name, dtype={'Vendor': str})
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