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

Absolute number is coming with decimals using pandas

I’m using pandas to apply some format level changes on a csv and storing the result in a target file. The source file has some integers, but after pandas operation the integers are converted to decimals. For e.g. 3 in source file converted to 3.0. I would like the integers remain as integers.

Any pointers on how to get this working? Will be really helpful, thank you!

import pandas as pd
  
# reading the csv file
df = pd.read_csv(source)
  
# updating the column value/data
df['Regular'] = df['Regular'].replace({',': '_,'})
  
# writing into the file
df.to_csv(target, index=False)

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

>Solution :

You can specify data type for pandas read_csv(), eg.:

df = pd.read_csv(source, dtype={'column_name_a': 'Int32', 'column_name_b': 'Int32'})

see docs here :: https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html

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