pythonlistdataframecategorical-data

I want to convert the categorical variable to numerical in Python


I have a dataframe having categorical variables. I want to convert them to the numerical using the following logic:

I have 2 lists one contains the distinct categorical values in the column and the second list contains the values for each category. Now i need to map these values in place of those categorical values.

For Eg:

List_A = ['A','B','C','D','E']

List_B = [3,2,1,1,2]

I need to replace A with 3, B with 2, C and D with 1 and E with 2.

Is there any way to do this in Python.

I can do this by applying multiple for loops but I am looking for some easier way or some direct function if there is any.

Any help is very much appreciated, Thanks in Advance.


Solution

  • Create a mapping dict

    List_A = ['A','B','C','D','E',]
    
    List_B = [3,2,1,1,2]
    d=dict(zip(List_A, List_B))
    
    new_list=['A','B','C','D','E','A','B']
    new_mapped_list=[d[v] for v in new_list if v in d]
    new_mapped_list
    

    Or define a function and use map

    List_A = ['A','B','C','D','E',]
    
    List_B = [3,2,1,1,2]
    
    d=dict(zip(List_A, List_B))
    
    def mapper(value):
        if value in d:
            return d[value]
        return None
    
    new_list=['A','B','C','D','E','A','B']
    map(mapper,new_list)