{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Occupation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Introduction:\n",
"\n",
"Special thanks to: https://github.com/justmarkham for sharing the dataset and materials.\n",
"\n",
"### Step 1. Import the necessary libraries"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 2. Import the dataset from this [address](https://raw.githubusercontent.com/justmarkham/DAT8/master/data/u.user). "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 3. Assign it to a variable called users."
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"
\n",
" \n",
" \n",
" | \n",
" age | \n",
" gender | \n",
" occupation | \n",
" zip_code | \n",
"
\n",
" \n",
" | user_id | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" | 1 | \n",
" 24 | \n",
" M | \n",
" technician | \n",
" 85711 | \n",
"
\n",
" \n",
" | 2 | \n",
" 53 | \n",
" F | \n",
" other | \n",
" 94043 | \n",
"
\n",
" \n",
" | 3 | \n",
" 23 | \n",
" M | \n",
" writer | \n",
" 32067 | \n",
"
\n",
" \n",
" | 4 | \n",
" 24 | \n",
" M | \n",
" technician | \n",
" 43537 | \n",
"
\n",
" \n",
" | 5 | \n",
" 33 | \n",
" F | \n",
" other | \n",
" 15213 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" age gender occupation zip_code\n",
"user_id \n",
"1 24 M technician 85711\n",
"2 53 F other 94043\n",
"3 23 M writer 32067\n",
"4 24 M technician 43537\n",
"5 33 F other 15213"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"users = pd.read_table('https://raw.githubusercontent.com/justmarkham/DAT8/master/data/u.user', \n",
" sep='|', index_col='user_id')\n",
"users.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 4. Discover what is the mean age per occupation"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"occupation\n",
"administrator 38.746835\n",
"artist 31.392857\n",
"doctor 43.571429\n",
"educator 42.010526\n",
"engineer 36.388060\n",
"entertainment 29.222222\n",
"executive 38.718750\n",
"healthcare 41.562500\n",
"homemaker 32.571429\n",
"lawyer 36.750000\n",
"librarian 40.000000\n",
"marketing 37.615385\n",
"none 26.555556\n",
"other 34.523810\n",
"programmer 33.121212\n",
"retired 63.071429\n",
"salesman 35.666667\n",
"scientist 35.548387\n",
"student 22.081633\n",
"technician 33.148148\n",
"writer 36.311111\n",
"Name: age, dtype: float64"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"users.groupby('occupation').age.mean()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 5. Discover the Male ratio per occupation and sort it from the most to the least"
]
},
{
"cell_type": "code",
"execution_count": 150,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"doctor 100.000000\n",
"engineer 97.014925\n",
"technician 96.296296\n",
"retired 92.857143\n",
"programmer 90.909091\n",
"executive 90.625000\n",
"scientist 90.322581\n",
"entertainment 88.888889\n",
"lawyer 83.333333\n",
"salesman 75.000000\n",
"educator 72.631579\n",
"student 69.387755\n",
"other 65.714286\n",
"marketing 61.538462\n",
"writer 57.777778\n",
"none 55.555556\n",
"administrator 54.430380\n",
"artist 53.571429\n",
"librarian 43.137255\n",
"healthcare 31.250000\n",
"homemaker 14.285714\n",
"dtype: float64"
]
},
"execution_count": 150,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# create a function\n",
"def gender_to_numeric(x):\n",
" if x == 'M':\n",
" return 1\n",
" if x == 'F':\n",
" return 0\n",
"\n",
"# apply the function to the gender column and create a new column\n",
"users['gender_n'] = users['gender'].apply(gender_to_numeric)\n",
"\n",
"\n",
"a = users.groupby('occupation').gender_n.sum() / users.occupation.value_counts() * 100 \n",
"\n",
"# sort to the most male \n",
"a.sort_values(ascending = False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 6. For each occupation, calculate the minimum and maximum ages"
]
},
{
"cell_type": "code",
"execution_count": 151,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" min | \n",
" max | \n",
"
\n",
" \n",
" | occupation | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" | administrator | \n",
" 21 | \n",
" 70 | \n",
"
\n",
" \n",
" | artist | \n",
" 19 | \n",
" 48 | \n",
"
\n",
" \n",
" | doctor | \n",
" 28 | \n",
" 64 | \n",
"
\n",
" \n",
" | educator | \n",
" 23 | \n",
" 63 | \n",
"
\n",
" \n",
" | engineer | \n",
" 22 | \n",
" 70 | \n",
"
\n",
" \n",
" | entertainment | \n",
" 15 | \n",
" 50 | \n",
"
\n",
" \n",
" | executive | \n",
" 22 | \n",
" 69 | \n",
"
\n",
" \n",
" | healthcare | \n",
" 22 | \n",
" 62 | \n",
"
\n",
" \n",
" | homemaker | \n",
" 20 | \n",
" 50 | \n",
"
\n",
" \n",
" | lawyer | \n",
" 21 | \n",
" 53 | \n",
"
\n",
" \n",
" | librarian | \n",
" 23 | \n",
" 69 | \n",
"
\n",
" \n",
" | marketing | \n",
" 24 | \n",
" 55 | \n",
"
\n",
" \n",
" | none | \n",
" 11 | \n",
" 55 | \n",
"
\n",
" \n",
" | other | \n",
" 13 | \n",
" 64 | \n",
"
\n",
" \n",
" | programmer | \n",
" 20 | \n",
" 63 | \n",
"
\n",
" \n",
" | retired | \n",
" 51 | \n",
" 73 | \n",
"
\n",
" \n",
" | salesman | \n",
" 18 | \n",
" 66 | \n",
"
\n",
" \n",
" | scientist | \n",
" 23 | \n",
" 55 | \n",
"
\n",
" \n",
" | student | \n",
" 7 | \n",
" 42 | \n",
"
\n",
" \n",
" | technician | \n",
" 21 | \n",
" 55 | \n",
"
\n",
" \n",
" | writer | \n",
" 18 | \n",
" 60 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" min max\n",
"occupation \n",
"administrator 21 70\n",
"artist 19 48\n",
"doctor 28 64\n",
"educator 23 63\n",
"engineer 22 70\n",
"entertainment 15 50\n",
"executive 22 69\n",
"healthcare 22 62\n",
"homemaker 20 50\n",
"lawyer 21 53\n",
"librarian 23 69\n",
"marketing 24 55\n",
"none 11 55\n",
"other 13 64\n",
"programmer 20 63\n",
"retired 51 73\n",
"salesman 18 66\n",
"scientist 23 55\n",
"student 7 42\n",
"technician 21 55\n",
"writer 18 60"
]
},
"execution_count": 151,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"users.groupby('occupation').age.agg(['min', 'max'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 7. For each combination of occupation and gender, calculate the mean age"
]
},
{
"cell_type": "code",
"execution_count": 152,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"occupation gender\n",
"administrator F 40.638889\n",
" M 37.162791\n",
"artist F 30.307692\n",
" M 32.333333\n",
"doctor M 43.571429\n",
"educator F 39.115385\n",
" M 43.101449\n",
"engineer F 29.500000\n",
" M 36.600000\n",
"entertainment F 31.000000\n",
" M 29.000000\n",
"executive F 44.000000\n",
" M 38.172414\n",
"healthcare F 39.818182\n",
" M 45.400000\n",
"homemaker F 34.166667\n",
" M 23.000000\n",
"lawyer F 39.500000\n",
" M 36.200000\n",
"librarian F 40.000000\n",
" M 40.000000\n",
"marketing F 37.200000\n",
" M 37.875000\n",
"none F 36.500000\n",
" M 18.600000\n",
"other F 35.472222\n",
" M 34.028986\n",
"programmer F 32.166667\n",
" M 33.216667\n",
"retired F 70.000000\n",
" M 62.538462\n",
"salesman F 27.000000\n",
" M 38.555556\n",
"scientist F 28.333333\n",
" M 36.321429\n",
"student F 20.750000\n",
" M 22.669118\n",
"technician F 38.000000\n",
" M 32.961538\n",
"writer F 37.631579\n",
" M 35.346154\n",
"Name: age, dtype: float64"
]
},
"execution_count": 152,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"users.groupby(['occupation', 'gender']).age.mean()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 8. For each occupation present the percentage of women and men"
]
},
{
"cell_type": "code",
"execution_count": 154,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"occupation gender\n",
"administrator F 45.569620\n",
" M 54.430380\n",
"artist F 46.428571\n",
" M 53.571429\n",
"doctor M 100.000000\n",
"educator F 27.368421\n",
" M 72.631579\n",
"engineer F 2.985075\n",
" M 97.014925\n",
"entertainment F 11.111111\n",
" M 88.888889\n",
"executive F 9.375000\n",
" M 90.625000\n",
"healthcare F 68.750000\n",
" M 31.250000\n",
"homemaker F 85.714286\n",
" M 14.285714\n",
"lawyer F 16.666667\n",
" M 83.333333\n",
"librarian F 56.862745\n",
" M 43.137255\n",
"marketing F 38.461538\n",
" M 61.538462\n",
"none F 44.444444\n",
" M 55.555556\n",
"other F 34.285714\n",
" M 65.714286\n",
"programmer F 9.090909\n",
" M 90.909091\n",
"retired F 7.142857\n",
" M 92.857143\n",
"salesman F 25.000000\n",
" M 75.000000\n",
"scientist F 9.677419\n",
" M 90.322581\n",
"student F 30.612245\n",
" M 69.387755\n",
"technician F 3.703704\n",
" M 96.296296\n",
"writer F 42.222222\n",
" M 57.777778\n",
"Name: gender, dtype: float64"
]
},
"execution_count": 154,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# create a data frame and apply count to gender\n",
"gender_ocup = users.groupby(['occupation', 'gender']).agg({'gender': 'count'})\n",
"\n",
"# create a DataFrame and apply count for each occupation\n",
"occup_count = users.groupby(['occupation']).agg('count')\n",
"\n",
"# divide the gender_ocup per the occup_count and multiply per 100\n",
"occup_gender = gender_ocup.div(occup_count, level = \"occupation\") * 100\n",
"\n",
"# present all rows from the 'gender column'\n",
"occup_gender.loc[: , 'gender']"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}