Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "`pivot_table`의 index나 columns 파라미터에 list를 넣어주면 multi-level index/column을 만들 수 있다."
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": 11,
  13. "metadata": {},
  14. "outputs": [
  15. {
  16. "data": {
  17. "text/html": [
  18. "<div>\n",
  19. "<style scoped>\n",
  20. " .dataframe tbody tr th:only-of-type {\n",
  21. " vertical-align: middle;\n",
  22. " }\n",
  23. "\n",
  24. " .dataframe tbody tr th {\n",
  25. " vertical-align: top;\n",
  26. " }\n",
  27. "\n",
  28. " .dataframe thead th {\n",
  29. " text-align: right;\n",
  30. " }\n",
  31. "</style>\n",
  32. "<table border=\"1\" class=\"dataframe\">\n",
  33. " <thead>\n",
  34. " <tr style=\"text-align: right;\">\n",
  35. " <th></th>\n",
  36. " <th>Survived</th>\n",
  37. " <th>0</th>\n",
  38. " <th>1</th>\n",
  39. " </tr>\n",
  40. " <tr>\n",
  41. " <th>Pclass</th>\n",
  42. " <th>Sex</th>\n",
  43. " <th></th>\n",
  44. " <th></th>\n",
  45. " </tr>\n",
  46. " </thead>\n",
  47. " <tbody>\n",
  48. " <tr>\n",
  49. " <th rowspan=\"2\" valign=\"top\">1</th>\n",
  50. " <th>female</th>\n",
  51. " <td>3</td>\n",
  52. " <td>91</td>\n",
  53. " </tr>\n",
  54. " <tr>\n",
  55. " <th>male</th>\n",
  56. " <td>77</td>\n",
  57. " <td>45</td>\n",
  58. " </tr>\n",
  59. " <tr>\n",
  60. " <th rowspan=\"2\" valign=\"top\">2</th>\n",
  61. " <th>female</th>\n",
  62. " <td>6</td>\n",
  63. " <td>70</td>\n",
  64. " </tr>\n",
  65. " <tr>\n",
  66. " <th>male</th>\n",
  67. " <td>91</td>\n",
  68. " <td>17</td>\n",
  69. " </tr>\n",
  70. " <tr>\n",
  71. " <th rowspan=\"2\" valign=\"top\">3</th>\n",
  72. " <th>female</th>\n",
  73. " <td>72</td>\n",
  74. " <td>72</td>\n",
  75. " </tr>\n",
  76. " <tr>\n",
  77. " <th>male</th>\n",
  78. " <td>300</td>\n",
  79. " <td>47</td>\n",
  80. " </tr>\n",
  81. " </tbody>\n",
  82. "</table>\n",
  83. "</div>"
  84. ],
  85. "text/plain": [
  86. "Survived 0 1\n",
  87. "Pclass Sex \n",
  88. "1 female 3 91\n",
  89. " male 77 45\n",
  90. "2 female 6 70\n",
  91. " male 91 17\n",
  92. "3 female 72 72\n",
  93. " male 300 47"
  94. ]
  95. },
  96. "execution_count": 11,
  97. "metadata": {},
  98. "output_type": "execute_result"
  99. }
  100. ],
  101. "source": [
  102. "# 2-level index\n",
  103. "titanic.pivot_table(\"Counts\",[\"Pclass\",\"Sex\"],[\"Survived\"], aggfunc = np.sum)"
  104. ]
  105. },
  106. {
  107. "cell_type": "code",
  108. "execution_count": 12,
  109. "metadata": {},
  110. "outputs": [
  111. {
  112. "data": {
  113. "text/html": [
  114. "<div>\n",
  115. "<style scoped>\n",
  116. " .dataframe tbody tr th:only-of-type {\n",
  117. " vertical-align: middle;\n",
  118. " }\n",
  119. "\n",
  120. " .dataframe tbody tr th {\n",
  121. " vertical-align: top;\n",
  122. " }\n",
  123. "\n",
  124. " .dataframe thead tr th {\n",
  125. " text-align: left;\n",
  126. " }\n",
  127. "\n",
  128. " .dataframe thead tr:last-of-type th {\n",
  129. " text-align: right;\n",
  130. " }\n",
  131. "</style>\n",
  132. "<table border=\"1\" class=\"dataframe\">\n",
  133. " <thead>\n",
  134. " <tr>\n",
  135. " <th>Pclass</th>\n",
  136. " <th colspan=\"2\" halign=\"left\">1</th>\n",
  137. " <th colspan=\"2\" halign=\"left\">2</th>\n",
  138. " <th colspan=\"2\" halign=\"left\">3</th>\n",
  139. " </tr>\n",
  140. " <tr>\n",
  141. " <th>Sex</th>\n",
  142. " <th>female</th>\n",
  143. " <th>male</th>\n",
  144. " <th>female</th>\n",
  145. " <th>male</th>\n",
  146. " <th>female</th>\n",
  147. " <th>male</th>\n",
  148. " </tr>\n",
  149. " <tr>\n",
  150. " <th>Survived</th>\n",
  151. " <th></th>\n",
  152. " <th></th>\n",
  153. " <th></th>\n",
  154. " <th></th>\n",
  155. " <th></th>\n",
  156. " <th></th>\n",
  157. " </tr>\n",
  158. " </thead>\n",
  159. " <tbody>\n",
  160. " <tr>\n",
  161. " <th>0</th>\n",
  162. " <td>3</td>\n",
  163. " <td>77</td>\n",
  164. " <td>6</td>\n",
  165. " <td>91</td>\n",
  166. " <td>72</td>\n",
  167. " <td>300</td>\n",
  168. " </tr>\n",
  169. " <tr>\n",
  170. " <th>1</th>\n",
  171. " <td>91</td>\n",
  172. " <td>45</td>\n",
  173. " <td>70</td>\n",
  174. " <td>17</td>\n",
  175. " <td>72</td>\n",
  176. " <td>47</td>\n",
  177. " </tr>\n",
  178. " </tbody>\n",
  179. "</table>\n",
  180. "</div>"
  181. ],
  182. "text/plain": [
  183. "Pclass 1 2 3 \n",
  184. "Sex female male female male female male\n",
  185. "Survived \n",
  186. "0 3 77 6 91 72 300\n",
  187. "1 91 45 70 17 72 47"
  188. ]
  189. },
  190. "execution_count": 12,
  191. "metadata": {},
  192. "output_type": "execute_result"
  193. }
  194. ],
  195. "source": [
  196. "# 2-level columns\n",
  197. "titanic.pivot_table(\"Counts\",[\"Survived\"], [\"Pclass\",\"Sex\"], aggfunc = np.sum)"
  198. ]
  199. }
  200. ],
  201. "metadata": {
  202. "kernelspec": {
  203. "display_name": "Python 3",
  204. "language": "python",
  205. "name": "python3"
  206. },
  207. "language_info": {
  208. "codemirror_mode": {
  209. "name": "ipython",
  210. "version": 3
  211. },
  212. "file_extension": ".py",
  213. "mimetype": "text/x-python",
  214. "name": "python",
  215. "nbconvert_exporter": "python",
  216. "pygments_lexer": "ipython3",
  217. "version": "3.6.5"
  218. }
  219. },
  220. "nbformat": 4,
  221. "nbformat_minor": 2
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement