Home > database >  Transform dataframe column from numerical value to classification value
Transform dataframe column from numerical value to classification value

Time:06-09

Working with the database in the image, i want to change the values of the column weight:

[![enter image description here][1]][1]

I want to transform this column into a discrete value using the function (or similar) to be able to one hot encde later the column (same as female/male column that was before gender):

def classificator(value):
    if value < (173-83):
        return 0
    elif value < (173):
        return 1
    else:
        return 2

How i can transform (or add a new column) with the classified values onto the dataframe? [1]: https://i.stack.imgur.com/US1mA.png

CodePudding user response:

Use pandas.cut:

df['weight'] = pd.cut(df['weight'], bins=[0, 173-83, 173, float('inf')],
                      labels=[0, 1, 2], right=False)

With you custom function (much less efficient):

df['weight'] = df['weight'].apply(classificator)

CodePudding user response:

If you want to use your function then you can do:

df["weight_encoded"] = [classificator(v) for v in df.weight]

Otherwise I would suggest proper binning functions like pandas.cut()

  • Related