Skip to content

Instantly share code, notes, and snippets.

@Midnighter
Last active August 9, 2020 10:35
Show Gist options
  • Save Midnighter/bbcecff45fb4787f35d880c9df3f259a to your computer and use it in GitHub Desktop.
Save Midnighter/bbcecff45fb4787f35d880c9df3f259a to your computer and use it in GitHub Desktop.
Return value of a MultiIndex with a single level
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"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": [
"rng = np.random.RandomState(123)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame({\n",
" \"value\": rng.random_sample(10),\n",
" \"status\": \"optimal\"\n",
"}, index=pd.MultiIndex.from_tuples([(chr(i + 65),) for i in range(10)], names=[\"first\"]))"
]
},
{
"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>value</th>\n",
" <th>status</th>\n",
" </tr>\n",
" <tr>\n",
" <th>first</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>A</th>\n",
" <td>0.696469</td>\n",
" <td>optimal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B</th>\n",
" <td>0.286139</td>\n",
" <td>optimal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>C</th>\n",
" <td>0.226851</td>\n",
" <td>optimal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>D</th>\n",
" <td>0.551315</td>\n",
" <td>optimal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>E</th>\n",
" <td>0.719469</td>\n",
" <td>optimal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>F</th>\n",
" <td>0.423106</td>\n",
" <td>optimal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>G</th>\n",
" <td>0.980764</td>\n",
" <td>optimal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>H</th>\n",
" <td>0.684830</td>\n",
" <td>optimal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>I</th>\n",
" <td>0.480932</td>\n",
" <td>optimal</td>\n",
" </tr>\n",
" <tr>\n",
" <th>J</th>\n",
" <td>0.392118</td>\n",
" <td>optimal</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" value status\n",
"first \n",
"A 0.696469 optimal\n",
"B 0.286139 optimal\n",
"C 0.226851 optimal\n",
"D 0.551315 optimal\n",
"E 0.719469 optimal\n",
"F 0.423106 optimal\n",
"G 0.980764 optimal\n",
"H 0.684830 optimal\n",
"I 0.480932 optimal\n",
"J 0.392118 optimal"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"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>value</th>\n",
" <th>status</th>\n",
" </tr>\n",
" <tr>\n",
" <th>first</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>A</th>\n",
" <td>0.696469</td>\n",
" <td>optimal</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" value status\n",
"first \n",
"A 0.696469 optimal"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc[\"A\"]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"pandas.core.frame.DataFrame"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(df.loc[\"A\"])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"value 0.696469\n",
"status optimal\n",
"Name: (A,), dtype: object"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc[\"A\",]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"pandas.core.series.Series"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(df.loc[\"A\",])"
]
}
],
"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": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment