<jupyter_start><jupyter_text>Read the Data Read the data from the given two datasets, viz. credits.csv and movies_metadata.csv. We have details about cast and crew in 'credits.csv' dataset and other important details(title, genres, year etc.) in 'movies_metadata.csv' dataset. We can merge these two dataset to get overall information.<jupyter_code># 1st Dataset
df1 = pd.read_csv('credits.csv')
df1
# 2nd dataset
df2 = pd.read_csv('movies_metadata.csv')
df2
df2['release_date'] = pd.to_datetime(df2['release_date'], errors='coerce')
df2['year'] = df2['release_date'].dt.year
df2['year'].value_counts().sort_index()<jupyter_output><empty_output><jupyter_text>Feature Selection<jupyter_code># From meta dataset we need genres, id,title and year only 
# (Getting only 2017 movies as we already have movies up to the year 2016 in prep1data file.)
 
df21 = df2.loc[df2.year == 2017,['genres','id','title','year']]
df21['id'] = df21['id'].astype(int)
df3 = pd.merge(df21, df1, on='id')
df3
# abstract syntax tree(ast) library
# literal_eval safely evaluate an expression node or a Unicode or Latin-1 encoded string containing a Python expression
import ast
df3['genres'] = df3['genres'].map(lambda x: ast.literal_eval(x))
df3['cast'] = df3['cast'].map(lambda x: ast.literal_eval(x))
df3['crew'] = df3['crew'].map(lambda x: ast.literal_eval(x))
def make_genresList(x):
    gen = []
    st = " "
    for i in x:
        if i.get('name') == 'Science Fiction':
            scifi = 'Sci-Fi'
            gen.append(scifi)
        else:
            gen.append(i.get('name'))
    if gen == []:
        return np.NaN
    else:
        return (st.join(gen))
df3['genres_list'] = df3['genres'].map(lambda x: make_genresList(x))
df3['genres_list']
def get_actor1(x):
    casts = []
    for i in x:
        casts.append(i.get('name'))
    if casts == []:
        return np.NaN
    else:
        return (casts[0])
    
def get_actor2(x):
    casts = []
    for i in x:
        casts.append(i.get('name'))
    if casts == [] or len(casts)<=1:
        return np.NaN
    else:
        return (casts[1])
    
def get_actor3(x):
    casts = []
    for i in x:
        casts.append(i.get('name'))
    if casts == [] or len(casts)<=2:
        return np.NaN
    else:
        return (casts[2])
    
df3['actor_1_name'] = df3['cast'].map(lambda x: get_actor1(x))
df3['actor_2_name'] = df3['cast'].map(lambda x: get_actor2(x))
df3['actor_3_name'] = df3['cast'].map(lambda x: get_actor3(x))
df3['actor_1_name']
df3['actor_2_name']
df3['actor_3_name']
def get_directors(x):
    dt = []
    st = " "
    for i in x:
        if i.get('job') == 'Director':
            dt.append(i.get('name'))
    if dt == []:
        return np.NaN
    else:
        return (st.join(dt))

df3['director_name'] = df3['crew'].map(lambda x: get_directors(x))
df3['director_name']
df3 = df3.loc[:,['director_name','actor_1_name','actor_2_name','actor_3_name','genres_list','title']]
df3<jupyter_output><empty_output><jupyter_text>Handling Null values<jupyter_code>df3.isna().sum()
df4 = df3.dropna(how='any')
df4.isna().sum()
df4 = df4.rename(columns={'genres_list':'genres'})
df4 = df4.rename(columns={'title':'movie_title'})
df4['movie_title'] = df4['movie_title'].str.lower()<jupyter_output><empty_output><jupyter_text>Save the File<jupyter_code>df4.to_csv("prep2data.csv", index= False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16785502079619727, 'fraction_numerical': 0.027926322043969103, 'mean_word_length': 3.9955489614243325, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '565056', 'license': 'Python-2.0', 'n_tokens_mistral': 1290, 'n_tokens_neox': 1241, 'n_words': 269, 'path': 'Recommendation/2Preprocessing_2017Movies.ipynb', 'repo_name': 'MintuJohnsontj/HollywoodRecommenderSystem', 'size': '3366'}