<jupyter_start><jupyter_text>Occupation  Introduction:Special thanks to: https://github.com/justmarkham for sharing the dataset and materials. Step 1. Import the necessary libraries<jupyter_code>import pandas as pd<jupyter_output><empty_output><jupyter_text>Step 2. Import the dataset from this [address](https://raw.githubusercontent.com/justmarkham/DAT8/master/data/u.user).   Step 3. Assign it to a variable called users.<jupyter_code>url = 'https://raw.githubusercontent.com/justmarkham/DAT8/master/data/u.user'
users = pd.read_csv(url, sep='|', index_col='user_id')
users.head()<jupyter_output><empty_output><jupyter_text>Step 4. Discover what is the mean age per occupation<jupyter_code>users.groupby('occupation')['age'].mean()<jupyter_output><empty_output><jupyter_text>Step 5. Discover the Male ratio per occupation and sort it from the most to the least<jupyter_code>gender_mapper = {'M': 1, 'F': 0}
users['gender_n'] = users['gender'].map(gender_mapper)
users
a = users.groupby('occupation')['gender_n'].sum() / users['occupation'].value_counts() * 100
a.sort_values(ascending = False)
users.groupby(['occupation', 'gender']).count().groupby('occupation').apply(lambda x: x.iloc[0]/(x.iloc[0] + x.iloc[1]) if len(x) > 1 else x.iloc[0]/x.iloc[0])['age'].rename('freq').sort_values(ascending=False)*100<jupyter_output><empty_output><jupyter_text>Step 6. For each occupation, calculate the minimum and maximum ages<jupyter_code>users.groupby('occupation')['age'].agg(['min', 'max'])<jupyter_output><empty_output><jupyter_text>Step 7. For each combination of occupation and gender, calculate the mean age<jupyter_code>users.groupby(['occupation', 'gender'])['age'].mean().apply(lambda x: round(x))
users.groupby(['occupation', 'gender'])['age'].agg(['mean']).apply(lambda x: round(x))<jupyter_output><empty_output><jupyter_text>Step 8.  For each occupation present the percentage of women and men<jupyter_code>byoccup_gender = users.groupby(['occupation','gender'])
occup_gender_count = byoccup_gender.agg(count=pd.NamedAgg(aggfunc='count',column='gender'))

occup_count = users.groupby('occupation').agg(count=pd.NamedAgg(aggfunc='count',column='gender'))

gender_freq = occup_gender_count.div(occup_count, level='occupation')*100
display(gender_freq)
#messier way
male_percentages = users.groupby(['occupation', 'gender']).count().groupby('occupation').apply(lambda x: x.iloc[0]/(x.iloc[0] + x.iloc[1]) if len(x) > 1 else x.iloc[0]/x.iloc[0])['age'].rename('male_freq').sort_values(ascending=False)*100
female_percentages = (100 - male_percentages).rename('female_freq')
pd.concat([male_percentages, female_percentages],axis=1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16729182295573894, 'fraction_numerical': 0.015003750937734433, 'mean_word_length': 8.805147058823529, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 31, 'https://': 3, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '261752', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 944, 'n_tokens_neox': 911, 'n_words': 158, 'path': '03_Grouping/Occupation/Exercise.ipynb', 'repo_name': 'slcheungcasado/pandas_exercises', 'size': '2666'}