Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert the column with lists into one hot encoded columns?

Assume, there is one DataFrame such as following

import pandas as pd 
import numpy as np 

df = pd.DataFrame({'id':range(1,4), 
                   'items':[['A', 'B'], ['A', 'B', 'C'], ['A', 'C']]})
df
        id  items
        1   [A, B]
        2   [A, B, C]
        3   [A, C]

Is there an efficient way to convert above DataFrame into the following (one-hot encoded columns)? Many Thanks in advance!

   id   items       A   B   C
    1   [A, B]      1   1   0
    2   [A, B, C]   1   1   1
    3   [A, C]      1   0   1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Another possible solution, whose steps are:

  • First, the explode function is used to transform each item of a list-like to a row, replicating the index values.

  • Then, the pivot_table function is applied to reshape the data based on the unique values in the items column, aggregating the count of each id for every item. The fill_value=0 ensures that any missing combinations are filled with zeros.

  • The rename_axis method is used to remove the axis name for the columns.

  • Finally, reset_index is called to reset the index of the dataframe, turning the index into a column.

  • The original dataframe df is then merged with this transformed dataframe using the merge function.

df.merge(
    df.explode('items')
    .pivot_table(index='id', columns='items', values='id', aggfunc=len, 
                 fill_value=0)
    .rename_axis(None, axis=1).reset_index())

Output:

   id      items  A  B  C
0   1     [A, B]  1  1  0
1   2  [A, B, C]  1  1  1
2   3     [A, C]  1  0  1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading