'LabelEncoding a permutation of combination of columns

I'd like to create class labels for a permutation of two columns using sklearn's LabelEncoder(). How do I achieve the following behavior?

import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder

df = pd.read_csv("data.csv", sep=",")
df
#    A    B    
# 0  1  Yes 
# 1  2   No 
# 2  3  Yes 
# 3  4  Yes

I'd like to have the permutation of combination of A && B rather than encoding these two columns separately:

df['A'].astype('category')
#Categories (4, int64): [1, 2, 3, 4, ]

df['B'].astype('category')
#Categories (2, object): ['Yes','No']

#Column C should have 4 * 2 classes:
(1,Yes)=1  (1,No)=5
(2,Yes)=2  (2,No)=6
(3,Yes)=3  (3,No)=7
(4,Yes)=4  (4,No)=8

#Newdf
#    A    B  C    
# 0  1  Yes  1
# 1  2   No  6
# 2  3  Yes  3
# 3  4  Yes  4


Solution 1:[1]

We can create the mapping df with cross merge

out = df.merge(df[['B']].drop_duplicates().merge(df['A'].drop_duplicates(),how='cross').assign(C=lambda x : x.index+1))
Out[415]: 
   A    B  C
0  1  Yes  1
1  2   No  6
2  3  Yes  3
3  4  Yes  4

More info

df[['B']].drop_duplicates().merge(df['A'].drop_duplicates(),how='cross').assign(C=lambda x : x.index+1)
Out[417]: 
     B  A  C
0  Yes  1  1
1  Yes  2  2
2  Yes  3  3
3  Yes  4  4
4   No  1  5
5   No  2  6
6   No  3  7
7   No  4  8

Solution 2:[2]

You can create additional column merging values from 2 columns into one tuple. But LabelEncoder cannot encode the tuples, so additionally you need to get hash() of the tuple:

df['AB'] = df.apply(lambda row: hash((row['A'], row['B'])), axis=1)
le = LabelEncoder()
df['C'] = le.fit_transform(df['AB'])

However, if you want to preserve the exact labels order (that you specified), using LabelEncoder() doesn't make sense. You can simply compute the C column like that:

df['C'] = df['A'] + (df['B']=='No') * df['A'].max()

Output:

    A   B   C
0   1   Yes 1
1   2   No  6
2   3   Yes 3
3   4   Yes 4

EDIT:

If you want to keep the labels for missed combinations (e.g. (2, 'Yes')) and need a solution for arbitrary number of classes, you can use 2 LabelEncoder():

leA = LabelEncoder()
leB = LabelEncoder()
leA.fit(df['A'])
leB.fit(df['B'])
df['C'] = leA.transform(df['A']) + leA.classes_.size
leB.transform(df['B']) + 1 # if you want labels to start from 1

But in this case you cannot preserve the custom order, the list of labels will be automatically sorted, e.g. [1,2,3,4] and ['No','Yes'].

Output:

    A   B   C
0   1   Yes 5
1   2   No  2
2   3   Yes 7
3   4   Yes 8

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2