Skip to content

Instantly share code, notes, and snippets.

@EthanRosenthal
Last active May 6, 2019 19:29
Show Gist options
  • Save EthanRosenthal/f72b59fa66ff1e6020acd2d150dc002a to your computer and use it in GitHub Desktop.
Save EthanRosenthal/f72b59fa66ff1e6020acd2d150dc002a to your computer and use it in GitHub Desktop.
Slicing a MultiIndex does not preserve ordering
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": "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></th>\n",
" <th>price</th>\n",
" </tr>\n",
" <tr>\n",
" <th>category</th>\n",
" <th>product</th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">fruit</th>\n",
" <th>apple</th>\n",
" <td>0.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>orange</th>\n",
" <td>0.6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>vegetable</th>\n",
" <th>broccoli</th>\n",
" <td>0.7</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" price\n",
"category product \n",
"fruit apple 0.5\n",
" orange 0.6\n",
"vegetable broccoli 0.7"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.DataFrame({\n",
" \"category\": [\"fruit\", \"fruit\", \"vegetable\"],\n",
" \"product\": [\"apple\", \"orange\", \"broccoli\"],\n",
" \"price\": [0.5, 0.6, 0.7]\n",
"})\n",
"df = df.set_index([\"category\", \"product\"])\n",
"df"
]
},
{
"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></th>\n",
" <th>price</th>\n",
" <th>color</th>\n",
" </tr>\n",
" <tr>\n",
" <th>category</th>\n",
" <th>product</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">fruit</th>\n",
" <th>apple</th>\n",
" <td>0.5</td>\n",
" <td>green</td>\n",
" </tr>\n",
" <tr>\n",
" <th>orange</th>\n",
" <td>0.6</td>\n",
" <td>orange</td>\n",
" </tr>\n",
" <tr>\n",
" <th>vegetable</th>\n",
" <th>broccoli</th>\n",
" <td>0.7</td>\n",
" <td>red</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" price color\n",
"category product \n",
"fruit apple 0.5 green\n",
" orange 0.6 orange\n",
"vegetable broccoli 0.7 red"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc[pd.IndexSlice[:, [\"broccoli\", \"orange\", \"apple\"]], \"color\"] = [\"green\", \"orange\", \"red\"]\n",
"df"
]
},
{
"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></th>\n",
" <th>price</th>\n",
" <th>color</th>\n",
" </tr>\n",
" <tr>\n",
" <th>category</th>\n",
" <th>product</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">fruit</th>\n",
" <th>apple</th>\n",
" <td>0.5</td>\n",
" <td>green</td>\n",
" </tr>\n",
" <tr>\n",
" <th>orange</th>\n",
" <td>0.6</td>\n",
" <td>orange</td>\n",
" </tr>\n",
" <tr>\n",
" <th>vegetable</th>\n",
" <th>broccoli</th>\n",
" <td>0.7</td>\n",
" <td>red</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" price color\n",
"category product \n",
"fruit apple 0.5 green\n",
" orange 0.6 orange\n",
"vegetable broccoli 0.7 red"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc[pd.IndexSlice[:, [\"broccoli\", \"orange\", \"apple\"]], :]"
]
}
],
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment