{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 多级index" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "from pandas import Series, DataFrame" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1 a -0.610026\n", " b 1.649775\n", " c 0.835034\n", "2 a 0.899401\n", " b -0.685298\n", " c -1.846341\n", "dtype: float64" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 创建一个两级的 Series\n", "s1 = pd.Series(np.random.randn(6), index=[[1, 1, 1, 2, 2, 2], ['a', 'b', 'c', 'a', 'b', 'c']])\n", "s1" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "a -0.610026\n", "b 1.649775\n", "c 0.835034\n", "dtype: float64" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1[1]" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.6100255133809527" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1[1]['a']" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1 1.042715\n", "2 1.460387\n", "dtype: float64" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1[:, 'a']" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
abc
1-0.6100261.6497750.835034
20.899401-0.685298-1.846341
\n", "
" ], "text/plain": [ " a b c\n", "1 -0.610026 1.649775 0.835034\n", "2 0.899401 -0.685298 -1.846341" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 多级 Series 转换为 Da taFrame\n", "df1 = s1.unstack()\n", "df1" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
abc
0-0.6100261.6497750.835034
10.899401-0.685298-1.846341
\n", "
" ], "text/plain": [ " a b c\n", "0 -0.610026 1.649775 0.835034\n", "1 0.899401 -0.685298 -1.846341" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df2 = pd.DataFrame([s1[1], s1[2]])\n", "df2" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1 a -0.610026\n", " b 1.649775\n", " c 0.835034\n", "2 a 0.899401\n", " b -0.685298\n", " c -1.846341\n", "dtype: float64" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# DataFrame 转换为多级 Series\n", "s2 = df1.T.unstack()\n", "s2" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
bjshgz
8988
a10123
24567
b1891011
212131415
\n", "
" ], "text/plain": [ " bj sh gz\n", " 8 9 8 8\n", "a 1 0 1 2 3\n", " 2 4 5 6 7\n", "b 1 8 9 10 11\n", " 2 12 13 14 15" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 创建多级 index 的 Da taFrame\n", "df = pd.DataFrame(np.arange(16).reshape(4, 4), index=[['a', 'a', 'b', 'b'], [1, 2, 1, 2]], columns=[['bj', 'bj', 'sh', 'gz'], [8, 9, 8, 8]])\n", "df" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
89
a101
245
b189
21213
\n", "
" ], "text/plain": [ " 8 9\n", "a 1 0 1\n", " 2 4 5\n", "b 1 8 9\n", " 2 12 13" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df['bj']" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "a 1 0\n", " 2 4\n", "b 1 8\n", " 2 12\n", "Name: 8, dtype: int64" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df['bj'][8]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.6" } }, "nbformat": 4, "nbformat_minor": 2 }