I am attempting to encode lists of categories within a dataframe by factoring them. I will then be creating a matrix from this series of lists (normalizing them to a set length, creating a multidimensional array, and one-hot encoding the elements in the matrix).
However, the factors do not maintain consistency between the rows. This can be seen here:
>>> import pandas as pd
>>> df = pd.DataFrame({'A': [ ['Other', 'Male', 'Female', 'Male', 'Other'], ['Female', 'Other', 'Male'] ]})
>>> df['B'] = df.A.apply(lambda x: pd.factorize(x)[0])
>>> df
A B
0 [Other, Male, Female, Male, Other] [0, 1, 2, 1, 0]
1 [Female, Other, Male] [0, 1, 2]
Does anyone know how to maintain an encoding for this series that is the same among rows?