{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Examples**"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"d = {'num_legs': [4, 4, 2, 2],\n",
" 'num_wings': [0, 0, 2, 2],\n",
" 'class': ['mammal', 'mammal', 'bird', 'bird'],\n",
" 'animal': ['tiger', 'fox', 'penguin', 'sparrow'],\n",
" 'locomotion': ['walks', 'walks', 'walks', 'flies']}\n",
"df = pd.DataFrame(data=d)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" | \n",
" | \n",
" num_legs | \n",
" num_wings | \n",
"
\n",
" \n",
" class | \n",
" animal | \n",
" locomotion | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" mammal | \n",
" tiger | \n",
" walks | \n",
" 4 | \n",
" 0 | \n",
"
\n",
" \n",
" fox | \n",
" walks | \n",
" 4 | \n",
" 0 | \n",
"
\n",
" \n",
" bird | \n",
" penguin | \n",
" walks | \n",
" 2 | \n",
" 2 | \n",
"
\n",
" \n",
" sparrow | \n",
" flies | \n",
" 2 | \n",
" 2 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" num_legs num_wings\n",
"class animal locomotion \n",
"mammal tiger walks 4 0\n",
" fox walks 4 0\n",
"bird penguin walks 2 2\n",
" sparrow flies 2 2"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = df.set_index(['class', 'animal', 'locomotion'])\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get values at specified index"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" | \n",
" num_legs | \n",
" num_wings | \n",
"
\n",
" \n",
" animal | \n",
" locomotion | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" tiger | \n",
" walks | \n",
" 4 | \n",
" 0 | \n",
"
\n",
" \n",
" fox | \n",
" walks | \n",
" 4 | \n",
" 0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" num_legs num_wings\n",
"animal locomotion \n",
"tiger walks 4 0\n",
"fox walks 4 0"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.xs('mammal')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get values at several indexes:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" num_legs | \n",
" num_wings | \n",
"
\n",
" \n",
" locomotion | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" walks | \n",
" 4 | \n",
" 0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" num_legs num_wings\n",
"locomotion \n",
"walks 4 0"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.xs(('mammal', 'fox'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get values at specified index and level:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" | \n",
" num_legs | \n",
" num_wings | \n",
"
\n",
" \n",
" class | \n",
" locomotion | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" mammal | \n",
" walks | \n",
" 4 | \n",
" 0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" num_legs num_wings\n",
"class locomotion \n",
"mammal walks 4 0"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.xs('tiger', level=1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get values at several indexes and levels"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" num_legs | \n",
" num_wings | \n",
"
\n",
" \n",
" animal | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" penguin | \n",
" 2 | \n",
" 2 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" num_legs num_wings\n",
"animal \n",
"penguin 2 2"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.xs(('bird', 'walks'),\n",
" level=[0, 'locomotion'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get values at specified column and axis:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"class animal locomotion\n",
"mammal tiger walks 0\n",
" fox walks 0\n",
"bird penguin walks 2\n",
" sparrow flies 2\n",
"Name: num_wings, dtype: int64"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.xs('num_wings', axis=1)"
]
}
],
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}