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 ignore (or convert) "\N" in a CSV with Pandas?

I have a very large CSV file with many cells that have "\N" as the value. For example:

OBJ_1 OBJ_2 TCA
16908 37152 2019-07-29 01:13:37
37152 16908 2019-07-29 01:13:37
16908 37152 2019-07-29 01:13:37
\N 16908 2019-07-29 01:13:37
19483 23132 \N
22829 \N 2019-07-29 01:13:37

When I run the function to read the file: pd.read_csv("path")

I get the error: ValueError: could not convert string to float: '\\N'

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

How can I read a CSV file with "\N" values and have them either ignored or replaced with some default value (like zero or undefined)?

>Solution :

According to the docs, you can use the na_values argument to automatically convert these to NaNs, like this:

df = pd.read_csv("path", na_values="\\N")
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