'How can I remove the thousand comma separator when converting data frame columns? [duplicate]

Given the following data frame:

State,City,Population,Poverty_Rate,Median_Age, 
VA,XYZ,.,10.5%,42, 
MD,ABC,"12,345",8.9%,., 
NY,.,987,654,.,41, 
...

import pandas as pd
df = pd.read_csv("/path... /sample_data")

df.dtypes returns

State          Object
City           Object
Population     Object
Proverty_Rate  Object
Median_Age     Object

I attempt to convert the data type of appropriate columns to int or float:

df = df.astype({"Population": int, "Proverty_rate": float, "Median_Age": int })

I received

Value Error: invalid literal for int() with base 10: '12,345'

I suspect the comma separator is causing this problem. How can I remove those from my dataset?



Solution 1:[1]

There is an argument in Pandas DataFrame as pd.read_csv(thousands=',') which is set to None by default.

data = """
State   City    Population Poverty_Rate  Median_Age
VA      XYZ     500,00          10.5%         42
MD      ABC     12,345      8.9%          .
NY      .       987,654     .             41"""

from io import StringIO
import pandas as pd

df = pd.read_csv(StringIO(data),sep='\s+',thousands=',')

print(df)

  State City  Population Poverty_Rate Median_Age
0    VA  XYZ       50000        10.5%         42
1    MD  ABC       12345         8.9%          .
2    NY    .      987654            .         41

Ideally, what you need to do is replace the string markers and then coerce your string columns into integers/floats.

#using your dict.
int_cols = ({"Population": int, "Poverty_Rate": float, "Median_Age": int })

for col in int_cols.keys():
    df[col] = pd.to_numeric(df[col].astype(str).str.replace('%',''),errors='coerce')

print(df.dtypes)

State            object
City             object
Population        int64
Poverty_Rate    float64
Median_Age      float64
dtype: object


print(df)

  State City  Population  Poverty_Rate  Median_Age
0    VA  XYZ       50000          10.5        42.0
1    MD  ABC       12345           8.9         NaN
2    NY    .      987654           NaN        41.0

Solution 2:[2]

Could you try the following? First do a str.replace on the column before you cast it to an integer?

import pandas as pd

df = pd.DataFrame([
    {'value': '123,445'},
    {'value': '143,445,788'}
])
df['value'] = df['value'].str.replace(',', '').astype(int)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dilini Peiris
Solution 2 JQadrad