Skip to content

Instantly share code, notes, and snippets.

@AlJohri
Last active January 13, 2020 03:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlJohri/59c9762845519f999eb28fe45276f4c1 to your computer and use it in GitHub Desktop.
Save AlJohri/59c9762845519f999eb28fe45276f4c1 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Data 3-5: Python - Filtering"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>name</th>\n",
" <th>age</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Rachel</td>\n",
" <td>34</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Monica</td>\n",
" <td>34</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Phoebe</td>\n",
" <td>37</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name age\n",
"0 Rachel 34\n",
"1 Monica 34\n",
"2 Phoebe 37"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.DataFrame([\n",
" {\"name\": \"Rachel\", \"age\": 34},\n",
" {\"name\": \"Monica\", \"age\": 34},\n",
" {\"name\": \"Phoebe\", \"age\": 37}\n",
"])\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>name</th>\n",
" <th>age</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Rachel</td>\n",
" <td>34</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Monica</td>\n",
" <td>34</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name age\n",
"0 Rachel 34\n",
"1 Monica 34"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.query('age < 37')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>name</th>\n",
" <th>age</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Rachel</td>\n",
" <td>34</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Phoebe</td>\n",
" <td>37</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name age\n",
"0 Rachel 34\n",
"2 Phoebe 37"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"whitelist = ['Rachel', 'Phoebe']\n",
"df.query('name in @whitelist')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>name</th>\n",
" <th>age</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Rachel</td>\n",
" <td>34</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Phoebe</td>\n",
" <td>37</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name age\n",
"0 Rachel 34\n",
"2 Phoebe 37"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"blacklist = ['Monica']\n",
"df.query('name not in @blacklist')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Data 3-6: Python - Grouping"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>model</th>\n",
" <th>make</th>\n",
" <th>color</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Yaris</td>\n",
" <td>Toyota</td>\n",
" <td>red</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Auris</td>\n",
" <td>Toyota</td>\n",
" <td>red</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Camry</td>\n",
" <td>Toyota</td>\n",
" <td>green</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Prius</td>\n",
" <td>Toyota</td>\n",
" <td>yellow</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Civic</td>\n",
" <td>Honda</td>\n",
" <td>red</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" model make color\n",
"0 Yaris Toyota red\n",
"1 Auris Toyota red\n",
"2 Camry Toyota green\n",
"3 Prius Toyota yellow\n",
"4 Civic Honda red"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.DataFrame([\n",
" {\"model\": \"Yaris\", \"make\": \"Toyota\", \"color\": \"red\"},\n",
" {\"model\": \"Auris\", \"make\": \"Toyota\", \"color\": \"red\"},\n",
" {\"model\": \"Camry\", \"make\": \"Toyota\", \"color\": \"green\"},\n",
" {\"model\": \"Prius\", \"make\": \"Toyota\", \"color\": \"yellow\"},\n",
" {\"model\": \"Civic\", \"make\": \"Honda\", \"color\": \"red\"},\n",
" {\"model\": \"Model 3\", \"make\": \"Tesla\", \"color\": \"red\"}\n",
"])\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"make\n",
"Honda 1\n",
"Tesla 1\n",
"Toyota 4\n",
"Name: color, dtype: int64"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.groupby('make').color.count()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Data 3-7: Python - Filtering and Grouping"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"make\n",
"Honda 1\n",
"Tesla 1\n",
"Toyota 2\n",
"Name: color, dtype: int64"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.query('color == \"red\"').groupby('make').color.count()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Pivot Table Example"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th>color</th>\n",
" <th>green</th>\n",
" <th>red</th>\n",
" <th>yellow</th>\n",
" </tr>\n",
" <tr>\n",
" <th>make</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Honda</th>\n",
" <td>0.0</td>\n",
" <td>1.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Tesla</th>\n",
" <td>0.0</td>\n",
" <td>1.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Toyota</th>\n",
" <td>1.0</td>\n",
" <td>2.0</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"color green red yellow\n",
"make \n",
"Honda 0.0 1.0 0.0\n",
"Tesla 0.0 1.0 0.0\n",
"Toyota 1.0 2.0 1.0"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.pivot_table(index='make', columns=['color'], values='model', aggfunc='count').fillna(0)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment