{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\ndf=pd.read_csv('/kaggle/input/moviesmetadata/movies_metadata.csv',low_memory=False)","metadata":{"execution":{"iopub.status.busy":"2023-09-23T06:29:47.710276Z","iopub.execute_input":"2023-09-23T06:29:47.710765Z","iopub.status.idle":"2023-09-23T06:29:49.237042Z","shell.execute_reply.started":"2023-09-23T06:29:47.710729Z","shell.execute_reply":"2023-09-23T06:29:49.235145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2023-09-23T06:29:50.57466Z","iopub.execute_input":"2023-09-23T06:29:50.57516Z","iopub.status.idle":"2023-09-23T06:29:50.584486Z","shell.execute_reply.started":"2023-09-23T06:29:50.575118Z","shell.execute_reply":"2023-09-23T06:29:50.582947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df[['title','genres', 'release_date', 'runtime', 'vote_average','vote_count']]\ndf.head()\n","metadata":{"execution":{"iopub.status.busy":"2023-09-23T06:30:12.829917Z","iopub.execute_input":"2023-09-23T06:30:12.83041Z","iopub.status.idle":"2023-09-23T06:30:12.863728Z","shell.execute_reply.started":"2023-09-23T06:30:12.830361Z","shell.execute_reply":"2023-09-23T06:30:12.862034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\ndf['year'] = df['release_date'].apply(lambda x: str(x).split('-')[0] if x != np.nan else np.nan)\ndf['year']","metadata":{"execution":{"iopub.status.busy":"2023-09-23T06:30:41.514572Z","iopub.execute_input":"2023-09-23T06:30:41.514982Z","iopub.status.idle":"2023-09-23T06:30:41.577247Z","shell.execute_reply.started":"2023-09-23T06:30:41.514951Z","shell.execute_reply":"2023-09-23T06:30:41.575525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_int(x): \n    try:\n        return int(x) \n    except:\n        return 0","metadata":{"execution":{"iopub.status.busy":"2023-09-23T06:31:10.629895Z","iopub.execute_input":"2023-09-23T06:31:10.631209Z","iopub.status.idle":"2023-09-23T06:31:10.637661Z","shell.execute_reply.started":"2023-09-23T06:31:10.631157Z","shell.execute_reply":"2023-09-23T06:31:10.636359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['year'] = df['year'].apply(convert_int)","metadata":{"execution":{"iopub.status.busy":"2023-09-23T06:31:21.374862Z","iopub.execute_input":"2023-09-23T06:31:21.375305Z","iopub.status.idle":"2023-09-23T06:31:21.425459Z","shell.execute_reply.started":"2023-09-23T06:31:21.375272Z","shell.execute_reply":"2023-09-23T06:31:21.423737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['year']","metadata":{"execution":{"iopub.status.busy":"2023-09-23T06:31:26.479974Z","iopub.execute_input":"2023-09-23T06:31:26.480489Z","iopub.status.idle":"2023-09-23T06:31:26.492451Z","shell.execute_reply.started":"2023-09-23T06:31:26.480451Z","shell.execute_reply":"2023-09-23T06:31:26.49033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop('release_date', axis=1)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-23T06:31:35.885072Z","iopub.execute_input":"2023-09-23T06:31:35.885469Z","iopub.status.idle":"2023-09-23T06:31:35.910431Z","shell.execute_reply.started":"2023-09-23T06:31:35.885437Z","shell.execute_reply":"2023-09-23T06:31:35.908738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['genres'][0]","metadata":{"execution":{"iopub.status.busy":"2023-09-23T06:32:04.70952Z","iopub.execute_input":"2023-09-23T06:32:04.709963Z","iopub.status.idle":"2023-09-23T06:32:04.719516Z","shell.execute_reply.started":"2023-09-23T06:32:04.709925Z","shell.execute_reply":"2023-09-23T06:32:04.717873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.iloc[0]['genres']","metadata":{"execution":{"iopub.status.busy":"2023-09-23T06:32:05.579988Z","iopub.execute_input":"2023-09-23T06:32:05.580381Z","iopub.status.idle":"2023-09-23T06:32:05.587885Z","shell.execute_reply.started":"2023-09-23T06:32:05.58035Z","shell.execute_reply":"2023-09-23T06:32:05.586474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(df['genres'][0])","metadata":{"execution":{"iopub.status.busy":"2023-09-23T06:32:11.589288Z","iopub.execute_input":"2023-09-23T06:32:11.589711Z","iopub.status.idle":"2023-09-23T06:32:11.59822Z","shell.execute_reply.started":"2023-09-23T06:32:11.589678Z","shell.execute_reply":"2023-09-23T06:32:11.596902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ast import literal_eval\na = \"[1,2,3]\"\nprint(type(a))\n\nb = literal_eval(a)\nprint(type(b))\nprint(b)","metadata":{"execution":{"iopub.status.busy":"2023-09-23T06:32:46.099045Z","iopub.execute_input":"2023-09-23T06:32:46.09943Z","iopub.status.idle":"2023-09-23T06:32:46.106002Z","shell.execute_reply.started":"2023-09-23T06:32:46.099399Z","shell.execute_reply":"2023-09-23T06:32:46.104789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['genres'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-09-23T06:32:54.791291Z","iopub.execute_input":"2023-09-23T06:32:54.791723Z","iopub.status.idle":"2023-09-23T06:32:54.806577Z","shell.execute_reply.started":"2023-09-23T06:32:54.79169Z","shell.execute_reply":"2023-09-23T06:32:54.804967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['genres'] = df['genres'].apply(literal_eval)\ndf['genres'][0]","metadata":{"execution":{"iopub.status.busy":"2023-09-23T06:33:13.814833Z","iopub.execute_input":"2023-09-23T06:33:13.815376Z","iopub.status.idle":"2023-09-23T06:33:15.86238Z","shell.execute_reply.started":"2023-09-23T06:33:13.815313Z","shell.execute_reply":"2023-09-23T06:33:15.860978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['genres'] = df['genres'].apply(lambda x: [i['name'].lower() for i in x] )","metadata":{"execution":{"iopub.status.busy":"2023-09-23T06:33:24.573639Z","iopub.execute_input":"2023-09-23T06:33:24.574011Z","iopub.status.idle":"2023-09-23T06:33:24.681707Z","shell.execute_reply.started":"2023-09-23T06:33:24.573982Z","shell.execute_reply":"2023-09-23T06:33:24.680494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['genres']","metadata":{"execution":{"iopub.status.busy":"2023-09-23T06:33:31.284852Z","iopub.execute_input":"2023-09-23T06:33:31.285283Z","iopub.status.idle":"2023-09-23T06:33:31.298356Z","shell.execute_reply.started":"2023-09-23T06:33:31.285248Z","shell.execute_reply":"2023-09-23T06:33:31.296934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-23T06:34:00.705283Z","iopub.execute_input":"2023-09-23T06:34:00.705806Z","iopub.status.idle":"2023-09-23T06:34:00.72734Z","shell.execute_reply.started":"2023-09-23T06:34:00.705768Z","shell.execute_reply":"2023-09-23T06:34:00.72625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s = df.apply(lambda x: pd.Series(x['genres']),axis=1).stack().reset_index(level=1, drop=True)\ns","metadata":{"execution":{"iopub.status.busy":"2023-09-23T06:34:16.859632Z","iopub.execute_input":"2023-09-23T06:34:16.860072Z","iopub.status.idle":"2023-09-23T06:34:36.630412Z","shell.execute_reply.started":"2023-09-23T06:34:16.860037Z","shell.execute_reply":"2023-09-23T06:34:36.628842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s.name = 'genre'\ngen_df = df.drop('genres', axis=1).join(s)\ngen_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-23T06:34:42.465256Z","iopub.execute_input":"2023-09-23T06:34:42.465698Z","iopub.status.idle":"2023-09-23T06:34:42.511887Z","shell.execute_reply.started":"2023-09-23T06:34:42.465663Z","shell.execute_reply":"2023-09-23T06:34:42.510225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_chart(gen_df, percentile=0.8): \n    #Ask for preferred genres \n    print(\"Input preferred genre\")\n    genre = input()\n    \n    print(\"Input shortest duration\")\n    low_time = int(input())\n    \n    print(\"Input longest duration\")\n    high_time = int(input())\n    \n    print(\"Input earliest year\")\n    low_year = int(input())\n    \n    print(\"Input latest year\")\n    high_year = int(input())\n    \n    movies = gen_df.copy()\n    movies = movies[(movies['genre'] == genre) &\n                   (movies['runtime'] >= low_time) &\n                   (movies['runtime'] <= high_time) &\n                   (movies['year'] >= low_year) &\n                   (movies['year'] <= high_year)]\n    C = movies['vote_average'].mean()\n    m = movies['vote_count'].quantile(percentile)\n    q_movies = movies.copy().loc[movies['vote_count'] >= m]\n    q_movies['score'] = q_movies.apply(lambda x: (x['vote_count']/(x['vote_count']+m) * x['vote_average'])\n                                      + (m/(m+x['vote_count']) * C)\n                                      ,axis=1)\n    q_movies = q_movies.sort_values('score', ascending=False) \n    return q_movies","metadata":{"execution":{"iopub.status.busy":"2023-09-23T06:40:23.840495Z","iopub.execute_input":"2023-09-23T06:40:23.84108Z","iopub.status.idle":"2023-09-23T06:40:23.853424Z","shell.execute_reply.started":"2023-09-23T06:40:23.841017Z","shell.execute_reply":"2023-09-23T06:40:23.852009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv(\"metadata_clean.csv\",)","metadata":{"execution":{"iopub.status.busy":"2023-09-23T06:42:18.224677Z","iopub.execute_input":"2023-09-23T06:42:18.225122Z","iopub.status.idle":"2023-09-23T06:42:18.628789Z","shell.execute_reply.started":"2023-09-23T06:42:18.225088Z","shell.execute_reply":"2023-09-23T06:42:18.62717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mcdf = pd.read_csv(\"metadata_clean.csv\")\nmcdf.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-23T06:42:25.010621Z","iopub.execute_input":"2023-09-23T06:42:25.011589Z","iopub.status.idle":"2023-09-23T06:42:25.125514Z","shell.execute_reply.started":"2023-09-23T06:42:25.011521Z","shell.execute_reply":"2023-09-23T06:42:25.124146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}