PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 11, 2022

[FIXED] How to remove the .0 when reading CSV with Pandas

 August 11, 2022     csv, decimal, pandas, precision     No comments   

Issue

I have a CSV file im reading into pandas dataframe. All the numbers do not have any decimal places, but as soon as I read it into the dframe it adds a trailing zero to the number with a decimal.

1205 becomes 1205.0

How do I get rid of the 0 during pd.read_csv?

I know i can drop the .0 after it has been read into the dataframe, but i really need it not to happen at all.

I have tried float_precision='round_trip'

I have tried to force the dtype during read_csv

Some of the code i tried:

df = pd.read_csv('xxx.csv', header=None, dtype={'T': object,'Date': object,'VAL1': float, 'VAL2': float, 'VAL3': float, 'VAL4': float, 'VAL5': float})

OR

df = pd.read_csv('xxx.csv', header=None, float_precision='round_trip')

Solution

You said you've tried to force dtype during read_csv, but I don't see why the following wouldn't solve your problem:

pd.read_csv('xxx.csv', dtype=str)


Answered By - Roméo Després
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing