From a public dataset available on film rating I created a contingency table as follow.
Honestly I don't like all these "for-loops" I think the quality of the code can be definitely improved in a more pythonic way.
import pandas as pd import numpy as np movies_df = pd.read_csv("https://raw.githubusercontent.com/uomodellamansarda/GentleIntroduction2MLandDataScience/main/L10/movies%20(1).csv") # Getting series of lists by applying split operation. movies_df.genres = movies_df.genres.str.split('|') # Getting distinct genre types for generating columns of genre type. genre_columns = list(set([j for i in movies_df['genres'].tolist() for j in i])) # Iterating over every list to create and fill values into columns. for j in genre_columns: movies_df[j] = 0 for i in range(movies_df.shape): for j in genre_columns: if(j in movies_df['genres'].iloc[i]): movies_df.loc[i,j] = 1 print(movies_df)