{
"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": [
"s = pd.Series([2, 3, 4, 5], name='f1',\n",
" index=pd.Index(['p', 'q', 'r', 's'], name='idx'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Generate a DataFrame with default index."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" idx | \n",
" f1 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" p | \n",
" 2 | \n",
"
\n",
" \n",
" 1 | \n",
" q | \n",
" 3 | \n",
"
\n",
" \n",
" 2 | \n",
" r | \n",
" 4 | \n",
"
\n",
" \n",
" 3 | \n",
" s | \n",
" 5 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" idx f1\n",
"0 p 2\n",
"1 q 3\n",
"2 r 4\n",
"3 s 5"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.reset_index()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To specify the name of the new column use name."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" idx | \n",
" values | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" p | \n",
" 2 | \n",
"
\n",
" \n",
" 1 | \n",
" q | \n",
" 3 | \n",
"
\n",
" \n",
" 2 | \n",
" r | \n",
" 4 | \n",
"
\n",
" \n",
" 3 | \n",
" s | \n",
" 5 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" idx values\n",
"0 p 2\n",
"1 q 3\n",
"2 r 4\n",
"3 s 5"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.reset_index(name='values')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To generate a new Series with the default set drop to True."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 2\n",
"1 3\n",
"2 4\n",
"3 5\n",
"Name: f1, dtype: int64"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.reset_index(drop=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To update the Series in place, without generating a new one set inplace to True. Note that it also
\n",
"requires drop=True."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 2\n",
"1 3\n",
"2 4\n",
"3 5\n",
"Name: f1, dtype: int64"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.reset_index(inplace=True, drop=True)\n",
"s"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The level parameter is interesting for Series with a multi-level index."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"arrays = [np.array(['b1', 'b2', 's1', 's2']),\n",
" np.array(['one', 'two', 'one', 'two'])]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"s2 = pd.Series(\n",
" range(4), name='f1',\n",
" index=pd.MultiIndex.from_arrays(arrays,\n",
" names=['p', 'q']))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To remove a specific level from the Index, use level."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" p | \n",
" f1 | \n",
"
\n",
" \n",
" q | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" one | \n",
" b1 | \n",
" 0 | \n",
"
\n",
" \n",
" two | \n",
" b2 | \n",
" 1 | \n",
"
\n",
" \n",
" one | \n",
" s1 | \n",
" 2 | \n",
"
\n",
" \n",
" two | \n",
" s2 | \n",
" 3 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" p f1\n",
"q \n",
"one b1 0\n",
"two b2 1\n",
"one s1 2\n",
"two s2 3"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s2.reset_index(level='p')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If level is not set, all levels are removed from the Index."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" p | \n",
" q | \n",
" f1 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" b1 | \n",
" one | \n",
" 0 | \n",
"
\n",
" \n",
" 1 | \n",
" b2 | \n",
" two | \n",
" 1 | \n",
"
\n",
" \n",
" 2 | \n",
" s1 | \n",
" one | \n",
" 2 | \n",
"
\n",
" \n",
" 3 | \n",
" s2 | \n",
" two | \n",
" 3 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" p q f1\n",
"0 b1 one 0\n",
"1 b2 two 1\n",
"2 s1 one 2\n",
"3 s2 two 3"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s2.reset_index()"
]
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}