Given a DataFrame df1 as follows:
df1 = pd.DataFrame({
'col1': [1,2,3,4],
'col2': [['a', 'b'], ['c'], ['a', 'd', 'b'], ['e']]
})
Which looks like:
col1 col2
0 1 [a, b]
1 2 [c]
2 3 [a, d, b]
3 4 [e]
I want to convert col2 - a column where each cell is a list - into several columns (a, b, c, d, e), where the values are boolean entries defining whether that column name existed in the original list, in the given row.
The output should follow this form:
df2 = pd.DataFrame({
'col1': [1,2,3,4],
'a': [True, False, True, False],
'b': [True, False, True, False],
'c': [False, True, False, False],
'd': [False, False, True, False],
'e': [False, False, False, True]
})
Which looks like:
col1 a b c d e
0 1 True True False False False
1 2 False False True False False
2 3 True True False True False
3 4 False False False False True
What's a clean way to do this?