{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Ex3 - Getting and Knowing your Data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This time we are going to pull data directly from the internet.\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": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"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 and use the 'user_id' as index"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 4. See the first 25 entries"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"scrolled": true
},
"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",
" | 6 | \n",
" 42 | \n",
" M | \n",
" executive | \n",
" 98101 | \n",
"
\n",
" \n",
" | 7 | \n",
" 57 | \n",
" M | \n",
" administrator | \n",
" 91344 | \n",
"
\n",
" \n",
" | 8 | \n",
" 36 | \n",
" M | \n",
" administrator | \n",
" 05201 | \n",
"
\n",
" \n",
" | 9 | \n",
" 29 | \n",
" M | \n",
" student | \n",
" 01002 | \n",
"
\n",
" \n",
" | 10 | \n",
" 53 | \n",
" M | \n",
" lawyer | \n",
" 90703 | \n",
"
\n",
" \n",
" | 11 | \n",
" 39 | \n",
" F | \n",
" other | \n",
" 30329 | \n",
"
\n",
" \n",
" | 12 | \n",
" 28 | \n",
" F | \n",
" other | \n",
" 06405 | \n",
"
\n",
" \n",
" | 13 | \n",
" 47 | \n",
" M | \n",
" educator | \n",
" 29206 | \n",
"
\n",
" \n",
" | 14 | \n",
" 45 | \n",
" M | \n",
" scientist | \n",
" 55106 | \n",
"
\n",
" \n",
" | 15 | \n",
" 49 | \n",
" F | \n",
" educator | \n",
" 97301 | \n",
"
\n",
" \n",
" | 16 | \n",
" 21 | \n",
" M | \n",
" entertainment | \n",
" 10309 | \n",
"
\n",
" \n",
" | 17 | \n",
" 30 | \n",
" M | \n",
" programmer | \n",
" 06355 | \n",
"
\n",
" \n",
" | 18 | \n",
" 35 | \n",
" F | \n",
" other | \n",
" 37212 | \n",
"
\n",
" \n",
" | 19 | \n",
" 40 | \n",
" M | \n",
" librarian | \n",
" 02138 | \n",
"
\n",
" \n",
" | 20 | \n",
" 42 | \n",
" F | \n",
" homemaker | \n",
" 95660 | \n",
"
\n",
" \n",
" | 21 | \n",
" 26 | \n",
" M | \n",
" writer | \n",
" 30068 | \n",
"
\n",
" \n",
" | 22 | \n",
" 25 | \n",
" M | \n",
" writer | \n",
" 40206 | \n",
"
\n",
" \n",
" | 23 | \n",
" 30 | \n",
" F | \n",
" artist | \n",
" 48197 | \n",
"
\n",
" \n",
" | 24 | \n",
" 21 | \n",
" F | \n",
" artist | \n",
" 94533 | \n",
"
\n",
" \n",
" | 25 | \n",
" 39 | \n",
" M | \n",
" engineer | \n",
" 55107 | \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\n",
"6 42 M executive 98101\n",
"7 57 M administrator 91344\n",
"8 36 M administrator 05201\n",
"9 29 M student 01002\n",
"10 53 M lawyer 90703\n",
"11 39 F other 30329\n",
"12 28 F other 06405\n",
"13 47 M educator 29206\n",
"14 45 M scientist 55106\n",
"15 49 F educator 97301\n",
"16 21 M entertainment 10309\n",
"17 30 M programmer 06355\n",
"18 35 F other 37212\n",
"19 40 M librarian 02138\n",
"20 42 F homemaker 95660\n",
"21 26 M writer 30068\n",
"22 25 M writer 40206\n",
"23 30 F artist 48197\n",
"24 21 F artist 94533\n",
"25 39 M engineer 55107"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 5. See the last 10 entries"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"scrolled": true
},
"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",
" | 934 | \n",
" 61 | \n",
" M | \n",
" engineer | \n",
" 22902 | \n",
"
\n",
" \n",
" | 935 | \n",
" 42 | \n",
" M | \n",
" doctor | \n",
" 66221 | \n",
"
\n",
" \n",
" | 936 | \n",
" 24 | \n",
" M | \n",
" other | \n",
" 32789 | \n",
"
\n",
" \n",
" | 937 | \n",
" 48 | \n",
" M | \n",
" educator | \n",
" 98072 | \n",
"
\n",
" \n",
" | 938 | \n",
" 38 | \n",
" F | \n",
" technician | \n",
" 55038 | \n",
"
\n",
" \n",
" | 939 | \n",
" 26 | \n",
" F | \n",
" student | \n",
" 33319 | \n",
"
\n",
" \n",
" | 940 | \n",
" 32 | \n",
" M | \n",
" administrator | \n",
" 02215 | \n",
"
\n",
" \n",
" | 941 | \n",
" 20 | \n",
" M | \n",
" student | \n",
" 97229 | \n",
"
\n",
" \n",
" | 942 | \n",
" 48 | \n",
" F | \n",
" librarian | \n",
" 78209 | \n",
"
\n",
" \n",
" | 943 | \n",
" 22 | \n",
" M | \n",
" student | \n",
" 77841 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" age gender occupation zip_code\n",
"user_id \n",
"934 61 M engineer 22902\n",
"935 42 M doctor 66221\n",
"936 24 M other 32789\n",
"937 48 M educator 98072\n",
"938 38 F technician 55038\n",
"939 26 F student 33319\n",
"940 32 M administrator 02215\n",
"941 20 M student 97229\n",
"942 48 F librarian 78209\n",
"943 22 M student 77841"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 6. What is the number of observations in the dataset?"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"943"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 7. What is the number of columns in the dataset?"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 8. Print the name of all the columns."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"Index([u'age', u'gender', u'occupation', u'zip_code'], dtype='object')"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 9. How is the dataset indexed?"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"Int64Index([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,\n",
" ...\n",
" 934, 935, 936, 937, 938, 939, 940, 941, 942, 943],\n",
" dtype='int64', name=u'user_id', length=943)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 10. What is the data type of each column?"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"age int64\n",
"gender object\n",
"occupation object\n",
"zip_code object\n",
"dtype: object"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 11. Print only the occupation column"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"user_id\n",
"1 technician\n",
"2 other\n",
"3 writer\n",
"4 technician\n",
"5 other\n",
"6 executive\n",
"7 administrator\n",
"8 administrator\n",
"9 student\n",
"10 lawyer\n",
"11 other\n",
"12 other\n",
"13 educator\n",
"14 scientist\n",
"15 educator\n",
"16 entertainment\n",
"17 programmer\n",
"18 other\n",
"19 librarian\n",
"20 homemaker\n",
"21 writer\n",
"22 writer\n",
"23 artist\n",
"24 artist\n",
"25 engineer\n",
"26 engineer\n",
"27 librarian\n",
"28 writer\n",
"29 programmer\n",
"30 student\n",
" ... \n",
"914 other\n",
"915 entertainment\n",
"916 engineer\n",
"917 student\n",
"918 scientist\n",
"919 other\n",
"920 artist\n",
"921 student\n",
"922 administrator\n",
"923 student\n",
"924 other\n",
"925 salesman\n",
"926 entertainment\n",
"927 programmer\n",
"928 student\n",
"929 scientist\n",
"930 scientist\n",
"931 educator\n",
"932 educator\n",
"933 student\n",
"934 engineer\n",
"935 doctor\n",
"936 other\n",
"937 educator\n",
"938 technician\n",
"939 student\n",
"940 administrator\n",
"941 student\n",
"942 librarian\n",
"943 student\n",
"Name: occupation, dtype: object"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 12. How many different occupations there are in this dataset?"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"21"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 13. What is the most frequent occupation?"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"student 196\n",
"other 105\n",
"educator 95\n",
"administrator 79\n",
"engineer 67\n",
"Name: occupation, dtype: int64"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 14. Summarize the DataFrame."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" age | \n",
"
\n",
" \n",
" \n",
" \n",
" | count | \n",
" 943.000000 | \n",
"
\n",
" \n",
" | mean | \n",
" 34.051962 | \n",
"
\n",
" \n",
" | std | \n",
" 12.192740 | \n",
"
\n",
" \n",
" | min | \n",
" 7.000000 | \n",
"
\n",
" \n",
" | 25% | \n",
" 25.000000 | \n",
"
\n",
" \n",
" | 50% | \n",
" 31.000000 | \n",
"
\n",
" \n",
" | 75% | \n",
" 43.000000 | \n",
"
\n",
" \n",
" | max | \n",
" 73.000000 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" age\n",
"count 943.000000\n",
"mean 34.051962\n",
"std 12.192740\n",
"min 7.000000\n",
"25% 25.000000\n",
"50% 31.000000\n",
"75% 43.000000\n",
"max 73.000000"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 15. Summarize all the columns"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" age | \n",
" gender | \n",
" occupation | \n",
" zip_code | \n",
"
\n",
" \n",
" \n",
" \n",
" | count | \n",
" 943.000000 | \n",
" 943 | \n",
" 943 | \n",
" 943 | \n",
"
\n",
" \n",
" | unique | \n",
" NaN | \n",
" 2 | \n",
" 21 | \n",
" 795 | \n",
"
\n",
" \n",
" | top | \n",
" NaN | \n",
" M | \n",
" student | \n",
" 55414 | \n",
"
\n",
" \n",
" | freq | \n",
" NaN | \n",
" 670 | \n",
" 196 | \n",
" 9 | \n",
"
\n",
" \n",
" | mean | \n",
" 34.051962 | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" | std | \n",
" 12.192740 | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" | min | \n",
" 7.000000 | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" | 25% | \n",
" 25.000000 | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" | 50% | \n",
" 31.000000 | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" | 75% | \n",
" 43.000000 | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" | max | \n",
" 73.000000 | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" age gender occupation zip_code\n",
"count 943.000000 943 943 943\n",
"unique NaN 2 21 795\n",
"top NaN M student 55414\n",
"freq NaN 670 196 9\n",
"mean 34.051962 NaN NaN NaN\n",
"std 12.192740 NaN NaN NaN\n",
"min 7.000000 NaN NaN NaN\n",
"25% 25.000000 NaN NaN NaN\n",
"50% 31.000000 NaN NaN NaN\n",
"75% 43.000000 NaN NaN NaN\n",
"max 73.000000 NaN NaN NaN"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 16. Summarize only the occupation column"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"count 943\n",
"unique 21\n",
"top student\n",
"freq 196\n",
"Name: occupation, dtype: object"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 17. What is the mean age of users?"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"34.0"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 18. What is the age with least occurrence?"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"7 1\n",
"Name: age, dtype: int64"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
}
],
"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
}