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

Thursday, August 11, 2022

[FIXED] how to handle decimal separator in float using Pandas?

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

Issue

So basically I have a csv file which consists of two columns in which the data are Cinema name and prices respectively. (data in Cinema name are all string whereas prices are float64 but may have example like 12,000.0 OR 3,025.54 where I want it to be 12000.0 or 3025.54 )
I firstly tried normal read_csv

df.read_csv('file')

But it turned out that the float64 was parsed as Object, which is not what I want. I read this post, but the solution there is assuming they know the column name and datatype in that column.
Assuming I don't know what the column name will be, how would I efficiently handle comma separator in float and make it into float instead of object ?
Note I only want to handle ',' only for float Data not String data.
Thanks for your helps..


Solution

Pandas read_csv function has a thousands argument, which you can specify to be , instead of the default .

df.read_csv('file', thousands=',')


Answered By - rafaelc
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • 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