Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.69 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "위의 titanic 데이터 프레임에서 \"Sex\"와 \"Pclass\"(객실등급)를 index와 columns으로 삼아 pivot을 해보도록 하자. <br>\n",
  8. "우선, 기존의 데이터프레임 중 두 개의 컬럼 \"Sex\"와 \"Pclass\"으로 구성된 새로운 데이터프레임을 만들어보자."
  9. ]
  10. },
  11. {
  12. "cell_type": "code",
  13. "execution_count": 3,
  14. "metadata": {},
  15. "outputs": [
  16. {
  17. "data": {
  18. "text/html": [
  19. "<div>\n",
  20. "<style scoped>\n",
  21. " .dataframe tbody tr th:only-of-type {\n",
  22. " vertical-align: middle;\n",
  23. " }\n",
  24. "\n",
  25. " .dataframe tbody tr th {\n",
  26. " vertical-align: top;\n",
  27. " }\n",
  28. "\n",
  29. " .dataframe thead th {\n",
  30. " text-align: right;\n",
  31. " }\n",
  32. "</style>\n",
  33. "<table border=\"1\" class=\"dataframe\">\n",
  34. " <thead>\n",
  35. " <tr style=\"text-align: right;\">\n",
  36. " <th></th>\n",
  37. " <th>Sex</th>\n",
  38. " <th>Pclass</th>\n",
  39. " </tr>\n",
  40. " </thead>\n",
  41. " <tbody>\n",
  42. " <tr>\n",
  43. " <th>886</th>\n",
  44. " <td>male</td>\n",
  45. " <td>2</td>\n",
  46. " </tr>\n",
  47. " <tr>\n",
  48. " <th>887</th>\n",
  49. " <td>female</td>\n",
  50. " <td>1</td>\n",
  51. " </tr>\n",
  52. " <tr>\n",
  53. " <th>888</th>\n",
  54. " <td>female</td>\n",
  55. " <td>3</td>\n",
  56. " </tr>\n",
  57. " <tr>\n",
  58. " <th>889</th>\n",
  59. " <td>male</td>\n",
  60. " <td>1</td>\n",
  61. " </tr>\n",
  62. " <tr>\n",
  63. " <th>890</th>\n",
  64. " <td>male</td>\n",
  65. " <td>3</td>\n",
  66. " </tr>\n",
  67. " </tbody>\n",
  68. "</table>\n",
  69. "</div>"
  70. ],
  71. "text/plain": [
  72. " Sex Pclass\n",
  73. "886 male 2\n",
  74. "887 female 1\n",
  75. "888 female 3\n",
  76. "889 male 1\n",
  77. "890 male 3"
  78. ]
  79. },
  80. "execution_count": 3,
  81. "metadata": {},
  82. "output_type": "execute_result"
  83. }
  84. ],
  85. "source": [
  86. "titanic_f1 = pd.DataFrame(titanic, columns=[\"Sex\", \"Pclass\"])\n",
  87. "titanic_f1.tail()"
  88. ]
  89. },
  90. {
  91. "cell_type": "markdown",
  92. "metadata": {},
  93. "source": [
  94. "titanic_f1 데이터프레임에는 특정 \"Pclass\" & 특정 \"Sex\" 조합에 해당하는 데이터가 여러 번 나타날 것이므로, 바로 pivot 메서드를 사용하게되면 에러가 발생하기 때문에 먼저 groupby를 해주어야 한다. \"Sex\"와 \"Pclass\"로 groupby를 해서 데이터의 수를 \"Counts\" 칼럼으로 만들어주자. <br>\n",
  95. "성별은 female & male, 객실등급은 1,2,3 등급이 있으므로 6가지 조합의 데이터 수를 결과로 얻게될 것이다."
  96. ]
  97. },
  98. {
  99. "cell_type": "code",
  100. "execution_count": 4,
  101. "metadata": {},
  102. "outputs": [
  103. {
  104. "data": {
  105. "text/html": [
  106. "<div>\n",
  107. "<style scoped>\n",
  108. " .dataframe tbody tr th:only-of-type {\n",
  109. " vertical-align: middle;\n",
  110. " }\n",
  111. "\n",
  112. " .dataframe tbody tr th {\n",
  113. " vertical-align: top;\n",
  114. " }\n",
  115. "\n",
  116. " .dataframe thead th {\n",
  117. " text-align: right;\n",
  118. " }\n",
  119. "</style>\n",
  120. "<table border=\"1\" class=\"dataframe\">\n",
  121. " <thead>\n",
  122. " <tr style=\"text-align: right;\">\n",
  123. " <th></th>\n",
  124. " <th>Sex</th>\n",
  125. " <th>Pclass</th>\n",
  126. " <th>Counts</th>\n",
  127. " </tr>\n",
  128. " </thead>\n",
  129. " <tbody>\n",
  130. " <tr>\n",
  131. " <th>0</th>\n",
  132. " <td>female</td>\n",
  133. " <td>1</td>\n",
  134. " <td>94</td>\n",
  135. " </tr>\n",
  136. " <tr>\n",
  137. " <th>1</th>\n",
  138. " <td>female</td>\n",
  139. " <td>2</td>\n",
  140. " <td>76</td>\n",
  141. " </tr>\n",
  142. " <tr>\n",
  143. " <th>2</th>\n",
  144. " <td>female</td>\n",
  145. " <td>3</td>\n",
  146. " <td>144</td>\n",
  147. " </tr>\n",
  148. " <tr>\n",
  149. " <th>3</th>\n",
  150. " <td>male</td>\n",
  151. " <td>1</td>\n",
  152. " <td>122</td>\n",
  153. " </tr>\n",
  154. " <tr>\n",
  155. " <th>4</th>\n",
  156. " <td>male</td>\n",
  157. " <td>2</td>\n",
  158. " <td>108</td>\n",
  159. " </tr>\n",
  160. " <tr>\n",
  161. " <th>5</th>\n",
  162. " <td>male</td>\n",
  163. " <td>3</td>\n",
  164. " <td>347</td>\n",
  165. " </tr>\n",
  166. " </tbody>\n",
  167. "</table>\n",
  168. "</div>"
  169. ],
  170. "text/plain": [
  171. " Sex Pclass Counts\n",
  172. "0 female 1 94\n",
  173. "1 female 2 76\n",
  174. "2 female 3 144\n",
  175. "3 male 1 122\n",
  176. "4 male 2 108\n",
  177. "5 male 3 347"
  178. ]
  179. },
  180. "execution_count": 4,
  181. "metadata": {},
  182. "output_type": "execute_result"
  183. }
  184. ],
  185. "source": [
  186. "titanic_f1 = titanic.groupby([\"Sex\",\"Pclass\"]).size().reset_index(name=\"Counts\")\n",
  187. "titanic_f1"
  188. ]
  189. },
  190. {
  191. "cell_type": "markdown",
  192. "metadata": {},
  193. "source": [
  194. "이제 pivot 메서드를 통해 \"Sex\"를 index로, \"Pclass\"를 columns로 하고 \"Counts\"가 values인 새로운 데이터프레임을 만들 수 있다."
  195. ]
  196. },
  197. {
  198. "cell_type": "code",
  199. "execution_count": 5,
  200. "metadata": {},
  201. "outputs": [
  202. {
  203. "data": {
  204. "text/html": [
  205. "<div>\n",
  206. "<style scoped>\n",
  207. " .dataframe tbody tr th:only-of-type {\n",
  208. " vertical-align: middle;\n",
  209. " }\n",
  210. "\n",
  211. " .dataframe tbody tr th {\n",
  212. " vertical-align: top;\n",
  213. " }\n",
  214. "\n",
  215. " .dataframe thead th {\n",
  216. " text-align: right;\n",
  217. " }\n",
  218. "</style>\n",
  219. "<table border=\"1\" class=\"dataframe\">\n",
  220. " <thead>\n",
  221. " <tr style=\"text-align: right;\">\n",
  222. " <th>Pclass</th>\n",
  223. " <th>1</th>\n",
  224. " <th>2</th>\n",
  225. " <th>3</th>\n",
  226. " </tr>\n",
  227. " <tr>\n",
  228. " <th>Sex</th>\n",
  229. " <th></th>\n",
  230. " <th></th>\n",
  231. " <th></th>\n",
  232. " </tr>\n",
  233. " </thead>\n",
  234. " <tbody>\n",
  235. " <tr>\n",
  236. " <th>female</th>\n",
  237. " <td>94</td>\n",
  238. " <td>76</td>\n",
  239. " <td>144</td>\n",
  240. " </tr>\n",
  241. " <tr>\n",
  242. " <th>male</th>\n",
  243. " <td>122</td>\n",
  244. " <td>108</td>\n",
  245. " <td>347</td>\n",
  246. " </tr>\n",
  247. " </tbody>\n",
  248. "</table>\n",
  249. "</div>"
  250. ],
  251. "text/plain": [
  252. "Pclass 1 2 3\n",
  253. "Sex \n",
  254. "female 94 76 144\n",
  255. "male 122 108 347"
  256. ]
  257. },
  258. "execution_count": 5,
  259. "metadata": {},
  260. "output_type": "execute_result"
  261. }
  262. ],
  263. "source": [
  264. "titanic_f1.pivot(\"Sex\",\"Pclass\",\"Counts\")"
  265. ]
  266. }
  267. ],
  268. "metadata": {
  269. "kernelspec": {
  270. "display_name": "Python 3",
  271. "language": "python",
  272. "name": "python3"
  273. },
  274. "language_info": {
  275. "codemirror_mode": {
  276. "name": "ipython",
  277. "version": 3
  278. },
  279. "file_extension": ".py",
  280. "mimetype": "text/x-python",
  281. "name": "python",
  282. "nbconvert_exporter": "python",
  283. "pygments_lexer": "ipython3",
  284. "version": "3.6.5"
  285. }
  286. },
  287. "nbformat": 4,
  288. "nbformat_minor": 2
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement