Question: Can someone help me to generate a test case for thebelow code. import pandas as pd # Load the data for movies and ratings into
Can someone help me to generate a test case for thebelow code.
import pandas as pd
# Load the data for movies and ratings into separate DataFrames movies_df = pd.read_csv("movies.csv") ratings_df= pd.read_csv("ratings.csv") # Merge the movies and ratings DataFrames on the 'movieId' column result_df = pd.merge(movies_df, ratings_df, on='movieId')
# Group the data by 'movieId' and 'name' grouped_df = result_df.groupby(['movieId', 'title', 'genres'])
# Define a function to extract the 'userId' and 'rating' columns def extract_users_and_ratings(df): return pd.Series({'user': df['userId'].tolist(), 'rating': df['rating'].tolist()})
# Apply the function to the grouped data and join the results into separate 'user' and 'rating' columns result_df = grouped_df.apply(extract_users_and_ratings).reset_index() result_df = result_df.drop('user', axis=1) # Prompt the user for a movie id x = input('Enter a movie id: ') genres = movies_df[movies_df['movieId'] == int(x)]['genres'].iloc[0] ratings = ratings_df[ratings_df['movieId'] == int(x)]['rating'] selected_movie_genres_list = genres.split('|') #genres_list = genres.str.split('|') ratings_list = ratings.tolist() filter = result_df['genres'].apply(lambda x: any(genre in x for genre in selected_movie_genres_list))
# Use the filter to select the movies filtered_movies = result_df[filter] filter = filtered_movies['rating'].apply(lambda x: any(rating in x for rating in ratings_list))
# Use the filter to select the rows filtered_rows = filtered_movies[filter]
# Print the filtered rows #print(filtered_rows)
# Output the resulting DataFrame print(filtered_rows)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
