jeudi 24 juin 2021

How can I change non-numerical data to numerical values by using "if condition"?

I would like to change non-numerical data to numerical values in one column. I tried to use factorize; pd.factorize(df['Class'])[0] + 1. However, it separates the 'Class' column from the actual matrix (y1 matrix). Then, I also used pd.get_dummies(). But, it gave only 1 and 0 value for each different non-numerical value.

My question is, is there any way to change non-numerical data to numerical values starting from 1 to 4 in my case, for the data in one column? or any other suggestions that you would like to give.

In the 'Class' data, I have one column. 356 rows. The cells contain N, O, P and S letters.

Thank you very much for any help.

def my_function(y1):
    if y1 == "N":
        convert = 1,
    elif y1 == "A":
            convert = 2,
        elif y1 == "O":
                convert = 3,
                else:  
                    convert = 4
    return convert

Aucun commentaire:

Enregistrer un commentaire