2

I am trying to do one-hot encoding for these clustered data frames. (grouped by length). Been trying to use sklearn's encoder but it seems to regard each individual row as one category instead of multiple.

Example input:

 ID                    trace  length
 3              [A, B, C, C]       4
 4           [A, B, C, C, D]       5
 5        [A, B, C, C, D, E]       6
 24             [A, B, C, C]       4
 25          [A, B, C, C, D]       5
 ...                     ...     ...

Expected output :

ID     A  B  C  D  E    length
3      1  1  1  0  0         4
4      1  1  1  1  0         5
5      1  1  1  1  1         6
24     1  1  1  0  0         4
25     1  1  1  1  0         5
.... ..... .. ......
1
  • Is trace a list or a string? Can you provide the input as DataFrame? Commented Mar 8, 2022 at 20:46

1 Answer 1

3

IIUC, and if target contains lists, you could do:

(df.drop('trace',1)
   .join(df['trace']
         .apply('|'.join)
         .str.get_dummies()
        )
 )

or for in place modification of df:

df = (df.join(df.pop('trace')
              .apply('|'.join)
              .str.get_dummies())
      )

Or using explode and pivot_table:

(df.explode('trace')
   .assign(x=1)
   .pivot_table(index=['ID', 'length'], columns='trace', values='x', aggfunc='first')
   .fillna(0, downcast='infer')
   .reset_index()
 )

Output:

   ID  length  A  B  C  D  E
0   3       4  1  1  1  0  0
1   4       5  1  1  1  1  0
2   5       6  1  1  1  1  1
3  24       4  1  1  1  0  0
4  25       5  1  1  1  1  0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.