<jupyter_start><jupyter_text>Pandas Answers  Exercise 1<jupyter_code>import pandas as pd
population = pd.read_csv('data/population.csv')

population['desity_estimation'] = population['Population'] / population['Land area']

population['error'] = abs(population['desity_estimation'] - population['Density'])

population.head()
population['error_type'] = population['error'].apply(lambda x: 'over' if x>0 else 'under')

population.head()
population['proportional_error'] = population['error'].abs() / population['Density']
population.head()
population.sort_values(by='proportional_error', ascending=False, inplace=False).head().iloc[0]<jupyter_output><empty_output><jupyter_text>Exercise 2<jupyter_code>users = pd.read_csv('data/ml-1m/users.dat', delimiter='::', 
                    names=['user id', 'gender', 'age', 'occupation code', 'zip'])

ratings = pd.read_csv('data/ml-1m/ratings.dat', delimiter='::', 
                     names=['user id', 'movie id', 'rating', 'timestamp'])

movies = pd.read_csv('data/ml-1m/movies.dat', delimiter='::', 
                     names=['movie id', 'title', 'genre'])
users.head()
ratings.head()
movies.head()
users.set_index('user id', inplace=True)
movies.set_index('movie id', inplace=True)
full_data = ratings.join(users, on='user id').join(movies, on='movie id')
#full_data.groupby('movie id').count()
full_data['movie id'].value_counts()
top5 = full_data['title'].value_counts()[:5]
top5
active_titles = list(full_data['title'].value_counts()[full_data['title'].value_counts() > 250].index)
mask = full_data['title'].apply(lambda x: x in active_titles)

active_data = full_data[mask]
ratings_by_gender = active_data.groupby(['title', 'gender']).mean()[['rating']]
ratings_by_gender.reset_index(inplace=True)
female_ratings = ratings_by_gender[ratings_by_gender['gender'] == 'F'].sort_values(by='rating', ascending=False)

female_ratings[:5]
male_ratings = ratings_by_gender[ratings_by_gender['gender'] == 'M'].sort_values(by='rating', ascending=False)

male_ratings[:5]
joined_ratings = male_ratings.join(female_ratings.set_index('title'), on='title', lsuffix='_male', rsuffix='_female')

joined_ratings.drop(['gender_male', 'gender_female'], axis=1, inplace=True)

joined_ratings.head()
joined_ratings['difference_in_ratings'] = joined_ratings['rating_male'] - joined_ratings['rating_female']
joined_ratings.sort_values('difference_in_ratings', inplace=True)
joined_ratings.head(10)
joined_ratings.tail(10)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17720499395892067, 'fraction_numerical': 0.008054772452678213, 'mean_word_length': 7.808510638297872, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '430687', 'license': 'Apache-2.0', 'n_tokens_mistral': 904, 'n_tokens_neox': 852, 'n_words': 123, 'path': '2.1 - Pandas_Answers.ipynb', 'repo_name': 'thecheech/class', 'size': '2483'}