0

I am trying to find unique values from the age column in Dataframe and then like to replace commas with an empty string. However, some cells only have comma (no value) shown in the output, and I would like to replace that with a mean value of that column.

My code is as follows

def data(value):
    value=str(value)
    if "," in value:
        value=value.replace (",", " ")
        return float(value)
    else: 
        return float (value)
df["Age"]= df["Age"].apply (data)
df["Age"].unique()

Output is

array([24.        , 23.        , 34.        , 21.        , 2,5.        ,
       27.        , 3,1.        , 33.        , 18.        , 26.        ,
       22.        , 30.        , ,.        , ,        , 5,4.        ,
       20.        , 17.        , 43.        , 4,7.        , 29.        ,
       41.        , 45.        , 4,9.        , 53.        , 57.        ,
       25.45288729, 19.        , 38.        , 35.        , 16.        ,
       37.        , 15.        , 42.        , 46.        , ,.        ,
       36.        , 14.        , 39.        , 48.        , 52.        ,
       44.        , 55.        , 50.        , 71.        , 63.        ,
       51.        , 58.        , 13.        , ,.        , 75.        ,
       65.        , 56.        , ,.        , 68.        , 84.        ,
       12.        , 72.        , 59.        , 61.        , 70.        ,
       74.        , 62.        , 67.        , 69.        , 73.        ,
       66.        , 11.        , ,.        , 88.        , 96.        ,
       80.        , 10.        , 81.        , 77.        , 97.        , ]) 

I am not getting rid of commas here despite applying this function. Please tell me where I am wrong in this code.

Hendrik Wiese
  • 2,010
  • 3
  • 22
  • 49
hunny
  • 11
  • 2

0 Answers0