Blackhole
Blackhole

Reputation: 572

Transform dataframe column from numerical value to classification value

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.sstatic.net/US1mA.png

Upvotes: 0

Views: 60

Answers (2)

DPM
DPM

Reputation: 945

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()

Upvotes: 0

mozway
mozway

Reputation: 262284

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)

Upvotes: 0

Related Questions