I currently have the following data framework:
import pandas as pd df= pd.DataFrame({"ID" : ['1','2','3','4','5'], "col2" : [['a', 'b', 'c'], ['c', 'd', 'e', 'f'], ['f', 'b', 'f'], ['a', 'c', 'b'], ['b', 'a', 'b']]}) print(df) ID col2 0 1 [a, b, c] 1 2 [c, d, e, f] 2 3 [f, b, f] 3 4 [a, c, b] 4 5 [b, a, d]
I want to create a new dataframe with layouts for col2, for example:
ID abcdef 0 1 1 1 1 0 0 0 1 2 0 0 1 1 1 1 2 3 0 1 0 0 0 1 3 4 1 1 1 0 0 0 4 5 1 1 0 1 0 0
Using the following code generates different columns for each of the letters in the column list:
df2= df.col2.str.get_dummies(sep = ",") pd.concat([data['col1'], df], axis=1) ID abb] cc] dd] ef] [a [b [c [f 1 0 1 0 0 1 0 0 0 0 1 0 0 0 2 0 0 0 0 0 1 0 1 1 0 0 1 0 3 0 1 0 0 0 0 0 0 1 0 0 0 1 4 0 0 1 1 0 0 0 0 0 1 0 0 0 5 1 0 0 0 0 0 1 0 0 0 1 0 0
Using the following code generates different columns for each of the letters in the column list according to the position in which they are located. Do you have any ideas why you can get through this? The pd.get_dummies option also does not work.