Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "`pivot_table`의 index나 columns 파라미터에 list를 넣어주면 multi-level index/column을 만들 수 있다."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "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>Survived</th>\n",
- " <th>0</th>\n",
- " <th>1</th>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>Pclass</th>\n",
- " <th>Sex</th>\n",
- " <th></th>\n",
- " <th></th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th rowspan=\"2\" valign=\"top\">1</th>\n",
- " <th>female</th>\n",
- " <td>3</td>\n",
- " <td>91</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>male</th>\n",
- " <td>77</td>\n",
- " <td>45</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th rowspan=\"2\" valign=\"top\">2</th>\n",
- " <th>female</th>\n",
- " <td>6</td>\n",
- " <td>70</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>male</th>\n",
- " <td>91</td>\n",
- " <td>17</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th rowspan=\"2\" valign=\"top\">3</th>\n",
- " <th>female</th>\n",
- " <td>72</td>\n",
- " <td>72</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>male</th>\n",
- " <td>300</td>\n",
- " <td>47</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- "Survived 0 1\n",
- "Pclass Sex \n",
- "1 female 3 91\n",
- " male 77 45\n",
- "2 female 6 70\n",
- " male 91 17\n",
- "3 female 72 72\n",
- " male 300 47"
- ]
- },
- "execution_count": 11,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# 2-level index\n",
- "titanic.pivot_table(\"Counts\",[\"Pclass\",\"Sex\"],[\"Survived\"], aggfunc = np.sum)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "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 tr th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe thead tr:last-of-type th {\n",
- " text-align: right;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr>\n",
- " <th>Pclass</th>\n",
- " <th colspan=\"2\" halign=\"left\">1</th>\n",
- " <th colspan=\"2\" halign=\"left\">2</th>\n",
- " <th colspan=\"2\" halign=\"left\">3</th>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>Sex</th>\n",
- " <th>female</th>\n",
- " <th>male</th>\n",
- " <th>female</th>\n",
- " <th>male</th>\n",
- " <th>female</th>\n",
- " <th>male</th>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>Survived</th>\n",
- " <th></th>\n",
- " <th></th>\n",
- " <th></th>\n",
- " <th></th>\n",
- " <th></th>\n",
- " <th></th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>0</th>\n",
- " <td>3</td>\n",
- " <td>77</td>\n",
- " <td>6</td>\n",
- " <td>91</td>\n",
- " <td>72</td>\n",
- " <td>300</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>1</th>\n",
- " <td>91</td>\n",
- " <td>45</td>\n",
- " <td>70</td>\n",
- " <td>17</td>\n",
- " <td>72</td>\n",
- " <td>47</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- "Pclass 1 2 3 \n",
- "Sex female male female male female male\n",
- "Survived \n",
- "0 3 77 6 91 72 300\n",
- "1 91 45 70 17 72 47"
- ]
- },
- "execution_count": 12,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# 2-level columns\n",
- "titanic.pivot_table(\"Counts\",[\"Survived\"], [\"Pclass\",\"Sex\"], aggfunc = np.sum)"
- ]
- }
- ],
- "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.5"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement