Last active
January 8, 2023 20:45
-
-
Save Keiku/b3e0038a2d1145ea82eda8444b98c02a to your computer and use it in GitHub Desktop.
Convert number strings with commas in pandas DataFrame to float.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
So much struggle with astype() and to_numeric(). This totally rescued me from angst and massive frustration. Thank you!!
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
Thank you for comments. I have updated.