Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

One way to transform a list within a dataframe to a binary data type is to use the get_dummies() function from the pandas library. This function will create binary columns for each unique value in the list. Here is an example:

import pandas as pd

# create a sample dataframe with a list column
df = pd.DataFrame({'A': [1, 2, 3],
                   'B': [['apple', 'banana'], ['orange', 'banana'], ['apple', 'orange']]})

# transform the list column to binary data type
df = pd.concat([df.drop('B', axis=1), pd.get_dummies(df['B'].apply(pd.Series).stack()).sum(level=0)], axis=1)

print(df)

Output:

   A  apple  banana  orange
0  1      1       1       0
1  2      0       1       1
2  3      1       0       1

In this example, the get_dummies() function is applied to the 'B' column and the resulting binary columns are concatenated with the other columns in the dataframe. The resulting dataframe has binary columns for each unique value in the list.