У меня есть список списков элементов, подобных этому:
lgenre[8:15]
[['Action'],
['Action', 'Adventure', 'Thriller'],
['Comedy', 'Drama', 'Romance'],
['Comedy', 'Horror'],
['Animation', "Children's"],
['Drama'],
['Action', 'Adventure', 'Romance']]
Что я хочу:
id Action Adventure Thriller Comedy Drama Romance Horror Animation Children's
0 0 1 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0
2 2 0 0 0 1 1 1 0 0 0
3 3 0 0 0 1 0 0 1 0 0
4 4 0 0 0 0 0 0 0 1 1
5 5 0 0 0 0 1 0 0 0 0
6 6 1 1 0 0 0 1 0 0 0
Я пытался написать двойной цикл, который выглядит следующим образом:
stor=pd.DataFrame({'id':list(range(len(lgenre[8:15])))})
for num,list in enumerate(lgenre[8:15]):
for item in list:
try:
stor[item][num]=1
except:
stor[item]=0
stor[item][num]=1
Хотя он компилируется, его реализация слишком медленная.Есть ли эффективный способ сделать это?Есть ли лучший алгоритм или встроенный метод?