Turning multiple binary columns into categorical (with less columns) with Python Pandas

I want to turn these categories into values of categorical columns. The values in each category are the current binary columns present in the data frame. We have : A11, A12.. is a detail of A1 so if the value in A11 ==1 it will necessarily imply having A1==1 but the inverse is not valid. Respecting the following conditions :

  1. maximaum of existing types is 4

  2. if A11==1 value of type1 should be equal to 'A11' and we ignore 'A1'

  3. if A11==1 and A12==1 we keep both, each one in a different column and ignore 'A1'

  4. if A1==1 A11==0 A12==0 then type1 should be equal to 'A1' for not having a detailed info A1X

  5. if none is equal to 1 then NaN

What I have :

df_test=pd.DataFrame({'A1':[1,0,1,1],'A11':[1,0,1,0],'A12':[1,0,1,0],
                      'B1':[0,1,0,0],'B11':[0,1,0,0],
                      'C1':[1,1,0,0],
                      'D1':[0,1,0,1],'D11':[0,1,0,1],'D12':[0,0,0,1],
                      'E1':[0,1,0,1],'E11':[0,0,0,0],'E12':[0,1,0,0],'E13':[0,0,0,0]})
df_test

    A1 A11 A12 B1  B11  C1  D1 D11 D12  E1 E11 E12 E13
0   1   1   1   0   0   1   0   0   0   0   0   0   0
1   0   0   0   1   1   1   1   1   0   1   0   1   0
2   1   1   1   0   0   0   0   0   0   0   0   0   0
3   1   0   0   0   0   0   1   1   1   1   0   0   0

Desired result I want :

   type1    type2   type3   type4
0   A11     A12      C1      NaN
1   B11     C1      D11      E12
2   A11     A12     NaN      NaN
3   A1      D11     D12      E1
 

Topic categorical-encoding dataframe pandas python data-cleaning

Category Data Science

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.