Skip to content

Instantly share code, notes, and snippets.

@jbencook
Created January 14, 2021 14:29
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 jbencook/17c22dfd6e423b36c22ed6a434be65c0 to your computer and use it in GitHub Desktop.
Save jbencook/17c22dfd6e423b36c22ed6a434be65c0 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": [
{
"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>date</th>\n",
" <th>region</th>\n",
" <th>revenue</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1999-01-02</td>\n",
" <td>APAC</td>\n",
" <td>928</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1999-01-03</td>\n",
" <td>AMER</td>\n",
" <td>526</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1999-01-04</td>\n",
" <td>EMEA</td>\n",
" <td>497</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1999-01-06</td>\n",
" <td>APAC</td>\n",
" <td>135</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1999-01-07</td>\n",
" <td>APAC</td>\n",
" <td>829</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" date region revenue\n",
"0 1999-01-02 APAC 928\n",
"1 1999-01-03 AMER 526\n",
"2 1999-01-04 EMEA 497\n",
"3 1999-01-06 APAC 135\n",
"4 1999-01-07 APAC 829"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"\n",
"df = pd.read_csv(\"https://jbencook.com/data/dummy-sales.csv\").head()\n",
"df"
]
},
{
"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>date</th>\n",
" <th>revenue</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1999-01-03</td>\n",
" <td>526</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1999-01-06</td>\n",
" <td>135</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" date revenue\n",
"1 1999-01-03 526\n",
"3 1999-01-06 135"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.drop(columns='region', index=[0, 2, 4])"
]
},
{
"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>region</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>APAC</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>EMEA</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>APAC</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" region\n",
"0 APAC\n",
"2 EMEA\n",
"4 APAC"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.drop(index=df.index[1::2], columns=df.columns[::2])"
]
},
{
"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>region</th>\n",
" <th>revenue</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>APAC</td>\n",
" <td>928</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>AMER</td>\n",
" <td>526</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>EMEA</td>\n",
" <td>497</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>APAC</td>\n",
" <td>135</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>APAC</td>\n",
" <td>829</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" region revenue\n",
"0 APAC 928\n",
"1 AMER 526\n",
"2 EMEA 497\n",
"3 APAC 135\n",
"4 APAC 829"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"copied_df = df.copy()\n",
"\n",
"del copied_df[\"date\"]\n",
"copied_df"
]
},
{
"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>date</th>\n",
" <th>region</th>\n",
" <th>revenue</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1999-01-02</td>\n",
" <td>APAC</td>\n",
" <td>928</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1999-01-03</td>\n",
" <td>AMER</td>\n",
" <td>526</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1999-01-04</td>\n",
" <td>EMEA</td>\n",
" <td>497</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" date region revenue\n",
"0 1999-01-02 APAC 928\n",
"1 1999-01-03 AMER 526\n",
"2 1999-01-04 EMEA 497"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.drop_duplicates(subset='region')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# If you're still reading this, play around with df.dropna()!"
]
}
],
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment