Skip to content Skip to sidebar Skip to footer

How To Get Rid Of $ Signs From The Column Values In Python

My dataset has quite a few columns containing $values with comma e.g. $150,000.50. Once I import the datasets: datasets = pd.read_csv('salaries-by-college-type.csv') The imputer

Solution 1:

Suppose you have a csv that looks like this. Note: I don't really know what your csv looks like. Make sure to adjust the read_csv parameters accordingly. Most specifically, the sep parameter.

h1|h2
a|$1,000.99
b|$500,000.00

Use the converters argument in pd.read_csv Pass a dictionary with the name of the columns you want converted as the keys and the function that does the converting as the values.

pd.read_csv(
    'salaries-by-college-type.csv', sep='|',
    converters=dict(h2=lambda x: float(x.strip('$').replace(',', '')))
)

  h1h20a1000.991b500000.00

Or suppose you imported the dataframe already

df = pd.read_csv(
    'salaries-by-college-type.csv', sep='|'
)

Then use pd.Series.str.replace

df.h2 = df.h2.str.replace('[^\d\.]', '').astype(float)

df

  h1         h2
0  a    1000.99
1  b  500000.00

Or pd.DataFrame.replace

df.replace(dict(h2='[^\d\.]'), '', regex=True).astype(dict(h2=float))

  h1h20a1000.991b500000.00

Post a Comment for "How To Get Rid Of $ Signs From The Column Values In Python"