<jupyter_start><jupyter_text>Movies Mini-project---In the previous worksheet you converted an SQL relational database to a single pandas dataframe and downloaded it. You will be analysing it today.If you were unable to download the file, there is a copy located here: "https://github.com/lilaceri/Working-with-data-/blob/main/Data%20Sets%20for%20code%20divisio/movies.csv?raw=true"  Inspect the dataset ---<jupyter_code>url = 'https://github.com/lilaceri/Working-with-data-/blob/main/Data%20Sets%20for%20code%20divisio/movies.csv?raw=true'
def create_dataframe(url):
  import pandas as pd
  df = pd.read_csv(url)
  return df

movies_df = create_dataframe(url)

display(movies_df.info())
display(movies_df.describe())
display(movies_df.head())<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 368894 entries, 0 to 368893
Data columns (total 7 columns):
 #   Column      Non-Null Count   Dtype  
---  ------      --------------   -----  
 0   Unnamed: 0  368894 non-null  int64  
 1   first_name  368894 non-null  object 
 2   last_name   368894 non-null  object 
 3   name        368894 non-null  object 
 4   year        368894 non-null  int64  
 5   rank        113376 non-null  float64
 6   genre       368894 non-null  object 
dtypes: float64(1), int64(2), object(4)
memory usage: 19.7+ MB<jupyter_text>Clean the dataset ---<jupyter_code>movies_df = movies_df.drop(columns=['Unnamed: 0'])
movies_df.drop_duplicates(inplace=True)
movies_df.info()
movies_df.head()<jupyter_output><class 'pandas.core.frame.DataFrame'>
Int64Index: 368893 entries, 0 to 368893
Data columns (total 6 columns):
 #   Column      Non-Null Count   Dtype  
---  ------      --------------   -----  
 0   first_name  368893 non-null  object 
 1   last_name   368893 non-null  object 
 2   name        368893 non-null  object 
 3   year        368893 non-null  int64  
 4   rank        113376 non-null  float64
 5   genre       368893 non-null  object 
dtypes: float64(1), int64(1), object(4)
memory usage: 19.7+ MB<jupyter_text>How many movies of each genre are there?---<jupyter_code>def get_movies_by(df, column='genre'):
  df_grouped = df.groupby(column)['name'].count()
  return df_grouped

#display(get_movies_by(movies_df, ['year', 'rank']))
display(get_movies_by(movies_df))<jupyter_output><empty_output><jupyter_text>Which director has the highest ranked movies?---<jupyter_code>def get_highest_rank_movies(df):
  max_rank = df['rank'].max(skipna=True)
  df_filtered = df[df['rank'] == max_rank]
  return df_filtered

display(get_highest_rank_movies(movies_df))<jupyter_output><empty_output><jupyter_text>How many movies have ranks of over 9?---<jupyter_code>def get_above_rank_9_movies(df, rank=9):
  max_rank = rank
  df_filtered = df[df['rank'] > max_rank]
  return df_filtered

total_above_9_rank_movies = len(get_above_rank_9_movies(movies_df))
print(f'Total above 9 rank movies:    {total_above_9_rank_movies}')<jupyter_output>Total above 9 rank movies:    1483<jupyter_text>Plot a bar chart of mean rank and genre---<jupyter_code>def show_barchart_rank_genre(df):
  import matplotlib.pyplot as plt

  rank_genre_df = df[['rank', 'genre']]
  df_grouped = rank_genre_df.groupby('genre')['rank'].mean()
  # Draw the bar graph
  df_grouped.plot(x='genre', y=df_grouped, kind='bar')

  plt.xlabel('Genre')
  plt.ylabel('Rank')
  plt.title('The Mean Rank')
  #plt.xticks(rotation=45)
  plt.show()

show_barchart_rank_genre(movies_df)<jupyter_output><empty_output><jupyter_text>Plot a pie chart of how many movies of each genre there are ---<jupyter_code>def show_pieplot_name_genre(df):
  import matplotlib.pyplot as plt

  rank_genre_df = df[['name', 'genre']]
  df_grouped = rank_genre_df.groupby('genre')['name'].count()

  # Draw the pie plot
  plt.pie(df_grouped, labels=df_grouped.keys())
  plt.title('Movies by Genre')
  plt.show()

show_pieplot_name_genre(movies_df)<jupyter_output><empty_output><jupyter_text>Plot a graph showing the mean Rank for each year<jupyter_code>def show_barchart_rank_year(df):
  import matplotlib.pyplot as plt

  rank_year_df = df[['rank', 'year']]
  df_grouped = rank_year_df.groupby('year')['rank'].mean()
  # Draw the line graph
  df_grouped.plot(x='year', y=df_grouped, kind='line')

  plt.xlabel('Year')
  plt.ylabel('Rank')
  plt.title('The Mean Rank')
  plt.show()

show_barchart_rank_year(movies_df)<jupyter_output><empty_output><jupyter_text>What else can you find out from this dataset?---Make a plan of 3 further things you can do to interrogate and analyse this dataset Type your answer here  1.  Highest ranked 'Comedy' movies?2.  The year with the highest number of movies?3.  Highest ranked 'Thriller' movies?  Complete the tasks you have set out in the exercise above. ---<jupyter_code>def get_highest_ranked_comedy_movies(df):
  comedy_df = df[df['genre'] == 'Comedy']
  max_rank = comedy_df['rank'].max(skipna=True)
  df_filtered = comedy_df[comedy_df['rank'] == max_rank]
  df_filtered = df_filtered.sort_values(by='year', ascending=False)
  return df_filtered

display(get_highest_ranked_comedy_movies(movies_df))
def show_movie_count_year(df):
  import matplotlib.pyplot as plt

  movie_count_df = df[['name', 'year']]
  df_grouped = movie_count_df.groupby('year')['name'].count()
  # Draw the line graph
  df_grouped.plot(x='year', y=df_grouped, kind='line')
  plt.xlabel('Year')
  plt.ylabel('Count')
  plt.title('The Number Of Movies Released')
  plt.show()

show_movie_count_year(movies_df)
def get_highest_ranked_thriller_movies(df):
  thriller_df = df[df['genre'] == 'Thriller']
  max_rank = thriller_df['rank'].max(skipna=True)
  df_filtered = thriller_df[thriller_df['rank'] == max_rank]
  df_filtered = df_filtered.sort_values(by='year', ascending=False)
  return df_filtered

display(get_highest_ranked_thriller_movies(movies_df))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12663006177076183, 'fraction_numerical': 0.032086479066575156, 'mean_word_length': 4.805776892430279, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 37, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '292472', 'license': 'MIT', 'n_tokens_mistral': 2308, 'n_tokens_neox': 2147, 'n_words': 456, 'path': 'Worksheets/10_2_Movies_Project.ipynb', 'repo_name': 'Imran-Github22/Programming-For-Data', 'size': '5828'}