Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert number strings with commas in pandas DataFrame to float.
import pandas as pd
import locale
from locale import atof
df = pd.DataFrame([['1,200', '4,200'], ['7,000', '-0.03'], ['5', '0']])
# 0 1
# 0 1,200 4,200
# 1 7,000 -0.03
# 2 5 0
locale.setlocale(locale.LC_NUMERIC, '')
df.applymap(atof)
# 0 1
# 0 1200.0 4200.00
# 1 7000.0 -0.03
# 2 5.0 0.00
@holdenweb

This comment has been minimized.

Copy link

holdenweb commented Mar 3, 2018

Used this to answer a Stackoverflow question, with credit. Thanks!

@gjain75

This comment has been minimized.

Copy link

gjain75 commented Jun 5, 2020

this function works for dataframe as a whole, trying to use same solution for a column of dataframe, returns an error stating that "series object does not have such functionality". Any solution for individual columns - some columns are text and throw error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.