Skip to content

Instantly share code, notes, and snippets.

@Keiku
Last active January 8, 2023 20:45
Show Gist options
  • Save Keiku/b3e0038a2d1145ea82eda8444b98c02a to your computer and use it in GitHub Desktop.
Save Keiku/b3e0038a2d1145ea82eda8444b98c02a to your computer and use it in GitHub Desktop.
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']],
columns=['col1', 'col2'])
# col1 col2
# 0 1,200 4,200
# 1 7,000 -0.03
# 2 5 0
# Check dtypes
df.dtypes
# col1 object
# col2 object
# dtype: object
# Convert the entire DataFrame
locale.setlocale(locale.LC_NUMERIC, '')
df.applymap(atof)
# col1 col2
# 0 1200.0 4200.00
# 1 7000.0 -0.03
# 2 5.0 0.00
# Check dtypes
df.dtypes
# col1 float64
# col2 float64
# dtype: object
# Convert a specific column
df['col2'] = df['col2'].map(atof)
df.dtypes
# col1 object
# col2 float64
# dtype: object
@marvyn
Copy link

marvyn commented Oct 28, 2022

Very helpful, thank you!
A little tip:
If using pd.read_csv from a datafile, we can set the thousands parameter to ','.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment