Используйте str.get_dummies
:
df = df['genres'].str.get_dummies('|')
print (df)
Adventure Animation Children Comedy Drama Fantasy Romance
0 1 1 1 1 0 1 0
1 1 0 1 0 0 1 0
2 0 0 0 1 0 0 1
3 0 0 0 1 1 0 1
4 0 0 0 1 0 0 0
и, если необходимо, добавьте обратно, как новые столбцы, добавьте join
:
df = df.join(df.pop('genres').str.get_dummies('|'))
print (df)
movieId title ... Fantasy Romance
0 1 Toy Story (1995) ... 1 0
1 2 Jumanji (1995) ... 1 0
2 3 Grumpier Old Men (1995) ... 0 1
3 4 Waiting to Exhale (1995) ... 0 1
4 5 Father of the Bride Part II (1995) ... 0 0