Search code examples
pythonpython-3.xstringtraining-datapunctuation

Remove punctuations from list and convert string value to float in python


I want to remove the dollar signs and commas from the column and cast to float. This is what I do so far, it didn't work. Actually nothing changed. The data look like["$200,00","$1,000.00"..."$50.00"]

import pandas as pd
import string
y_train = train.iloc[:,-1]
needtoclean=y_train.to_list()#''.join(y_train.to_list())

to_delete = set(string.punctuation) - {'$',','} 
clean = [x for x in needtoclean if x not in to_delete]

Solution

  • list_ = ['$58.00', '$60.00']       #Your Lise
    new_list = []                      #Initialise new list
    for elem in list_:                 #Iterate over previous list's elements
        elem = elem.replace("$", '')   #Replace the `$` sign
        new_list.append(float(elem))   #Add the typecasted float to new list