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 I parse numbers with thousands separator in pandas read_csv?

I have a CSV file with lines as follows:

"Dec 30, 2021","1,234.11","1,654.22","11,876.23","1,676,234"

I have learn from a previous post that I can use:

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

parse_dates=['Date']

To get the date parsed (that works).
However I would like columns 2-4 as np.float64 and column 5 as int64. How can I achieve that?

I have tried this:

data = pd.read_csv("file.csv",  parse_dates=['Date'], dtype=[np.datetime64, np.float64, np.float64, np.float64, np.float64, np.int64])

but I get

TypeError: data type not understood

>Solution :

Use thousands parameter.

df = pd.read_csv("file.csv",  parse_dates=['Date'], thousands=',')
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