I am trying to convert a single pandas column that is made up of a list of strings (containing spaces) into a one-hot-encoded columns (inputted as a string as a result of reading from excel). I have tried the approaches discussed in the solutions to How to one-hot-encode from a pandas column containing a list? but they don't work in this scenario- the individual letters are broken into columns. The other condition is that sometimes the lists can be empty as well.
More concretely, for dataframe df
Col1 Col2 Col3
C 4 "['Chocolate cake', 'White wine', 'Peanuts']"
A 1.7 "[]"
B 1 "['Chocolate cake', 'Salmon']"
I would like to create:
new_df
Col1 Col2 Chocolate cake White wine Peanuts Salmon
C 4 1 1 1 0
A 1.7 0 0 0 0
B 1 1 0 0 1
What is a pythonic way to create this dataframe?
EDIT:
Code for generating the pandas dataframe.
pd.DataFrame.from_dict({'Col1':['C', 'A', 'B'], 'Col2':[4, 1.7, 1], 'Col3':["['Chocolate cake', 'White wine', 'Peanuts']", \
"[]", "['Chocolate cake', 'Salmon']" ]})