Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.77 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {},
  7. "outputs": [],
  8. "source": [
  9. "# 必要なライブラリをインポート\n",
  10. "import pandas as pd\n",
  11. "import numpy as np"
  12. ]
  13. },
  14. {
  15. "cell_type": "code",
  16. "execution_count": 2,
  17. "metadata": {},
  18. "outputs": [
  19. {
  20. "data": {
  21. "text/html": [
  22. "<div>\n",
  23. "<style scoped>\n",
  24. " .dataframe tbody tr th:only-of-type {\n",
  25. " vertical-align: middle;\n",
  26. " }\n",
  27. "\n",
  28. " .dataframe tbody tr th {\n",
  29. " vertical-align: top;\n",
  30. " }\n",
  31. "\n",
  32. " .dataframe thead th {\n",
  33. " text-align: right;\n",
  34. " }\n",
  35. "</style>\n",
  36. "<table border=\"1\" class=\"dataframe\">\n",
  37. " <thead>\n",
  38. " <tr style=\"text-align: right;\">\n",
  39. " <th></th>\n",
  40. " <th>key1</th>\n",
  41. " <th>key2</th>\n",
  42. " <th>data1</th>\n",
  43. " <th>data2</th>\n",
  44. " </tr>\n",
  45. " </thead>\n",
  46. " <tbody>\n",
  47. " <tr>\n",
  48. " <th>0</th>\n",
  49. " <td>A</td>\n",
  50. " <td>1Gr</td>\n",
  51. " <td>11284.702046</td>\n",
  52. " <td>106.384940</td>\n",
  53. " </tr>\n",
  54. " <tr>\n",
  55. " <th>1</th>\n",
  56. " <td>C</td>\n",
  57. " <td>2Gr</td>\n",
  58. " <td>8236.624741</td>\n",
  59. " <td>99.386840</td>\n",
  60. " </tr>\n",
  61. " <tr>\n",
  62. " <th>2</th>\n",
  63. " <td>C</td>\n",
  64. " <td>2Gr</td>\n",
  65. " <td>7631.274238</td>\n",
  66. " <td>113.127964</td>\n",
  67. " </tr>\n",
  68. " <tr>\n",
  69. " <th>3</th>\n",
  70. " <td>B</td>\n",
  71. " <td>1Gr</td>\n",
  72. " <td>11998.305120</td>\n",
  73. " <td>120.099518</td>\n",
  74. " </tr>\n",
  75. " <tr>\n",
  76. " <th>4</th>\n",
  77. " <td>D</td>\n",
  78. " <td>2Gr</td>\n",
  79. " <td>9401.879005</td>\n",
  80. " <td>100.945629</td>\n",
  81. " </tr>\n",
  82. " <tr>\n",
  83. " <th>5</th>\n",
  84. " <td>B</td>\n",
  85. " <td>1Gr</td>\n",
  86. " <td>9971.014043</td>\n",
  87. " <td>125.971079</td>\n",
  88. " </tr>\n",
  89. " </tbody>\n",
  90. "</table>\n",
  91. "</div>"
  92. ],
  93. "text/plain": [
  94. " key1 key2 data1 data2\n",
  95. "0 A 1Gr 11284.702046 106.384940\n",
  96. "1 C 2Gr 8236.624741 99.386840\n",
  97. "2 C 2Gr 7631.274238 113.127964\n",
  98. "3 B 1Gr 11998.305120 120.099518\n",
  99. "4 D 2Gr 9401.879005 100.945629\n",
  100. "5 B 1Gr 9971.014043 125.971079"
  101. ]
  102. },
  103. "execution_count": 2,
  104. "metadata": {},
  105. "output_type": "execute_result"
  106. }
  107. ],
  108. "source": [
  109. "# 適当なデータフレームを作成\n",
  110. "df = pd.DataFrame({'key1' : ['A', 'C', 'C', 'B', 'D', 'B'],\n",
  111. " 'key2' : ['1Gr', '2Gr', '2Gr', '1Gr', '2Gr', '1Gr'],\n",
  112. " 'data1': np.random.normal(10000, 2000, 6),\n",
  113. " 'data2': np.random.normal(100, 20, 6)})\n",
  114. "df"
  115. ]
  116. },
  117. {
  118. "cell_type": "code",
  119. "execution_count": 3,
  120. "metadata": {},
  121. "outputs": [],
  122. "source": [
  123. "# Groupby オブジェクト作成\n",
  124. "# data1 を key1 の値をもとにグループ分けされる\n",
  125. "gr_data1_key1 = df['data1'].groupby(df['key1'])"
  126. ]
  127. },
  128. {
  129. "cell_type": "code",
  130. "execution_count": 4,
  131. "metadata": {},
  132. "outputs": [
  133. {
  134. "data": {
  135. "text/plain": [
  136. "key1\n",
  137. "A 11284.702046\n",
  138. "B 10984.659582\n",
  139. "C 7933.949489\n",
  140. "D 9401.879005\n",
  141. "Name: data1, dtype: float64"
  142. ]
  143. },
  144. "execution_count": 4,
  145. "metadata": {},
  146. "output_type": "execute_result"
  147. }
  148. ],
  149. "source": [
  150. "# ke1 の値(A, B, C, D)毎に合計値を算出\n",
  151. "gr_data1_key1.mean()"
  152. ]
  153. },
  154. {
  155. "cell_type": "code",
  156. "execution_count": 5,
  157. "metadata": {},
  158. "outputs": [
  159. {
  160. "data": {
  161. "text/plain": [
  162. "key1\n",
  163. "A 11284.702046\n",
  164. "B 21969.319164\n",
  165. "C 15867.898978\n",
  166. "D 9401.879005\n",
  167. "Name: data1, dtype: float64"
  168. ]
  169. },
  170. "execution_count": 5,
  171. "metadata": {},
  172. "output_type": "execute_result"
  173. }
  174. ],
  175. "source": [
  176. "# ke1 の値(A, B, C, D)毎に合計値を算出\n",
  177. "gr_data1_key1.sum()"
  178. ]
  179. },
  180. {
  181. "cell_type": "code",
  182. "execution_count": 6,
  183. "metadata": {},
  184. "outputs": [
  185. {
  186. "data": {
  187. "text/html": [
  188. "<div>\n",
  189. "<style scoped>\n",
  190. " .dataframe tbody tr th:only-of-type {\n",
  191. " vertical-align: middle;\n",
  192. " }\n",
  193. "\n",
  194. " .dataframe tbody tr th {\n",
  195. " vertical-align: top;\n",
  196. " }\n",
  197. "\n",
  198. " .dataframe thead th {\n",
  199. " text-align: right;\n",
  200. " }\n",
  201. "</style>\n",
  202. "<table border=\"1\" class=\"dataframe\">\n",
  203. " <thead>\n",
  204. " <tr style=\"text-align: right;\">\n",
  205. " <th></th>\n",
  206. " <th>data1</th>\n",
  207. " <th>data2</th>\n",
  208. " </tr>\n",
  209. " <tr>\n",
  210. " <th>key1</th>\n",
  211. " <th></th>\n",
  212. " <th></th>\n",
  213. " </tr>\n",
  214. " </thead>\n",
  215. " <tbody>\n",
  216. " <tr>\n",
  217. " <th>A</th>\n",
  218. " <td>11284.702046</td>\n",
  219. " <td>106.384940</td>\n",
  220. " </tr>\n",
  221. " <tr>\n",
  222. " <th>B</th>\n",
  223. " <td>10984.659582</td>\n",
  224. " <td>123.035298</td>\n",
  225. " </tr>\n",
  226. " <tr>\n",
  227. " <th>C</th>\n",
  228. " <td>7933.949489</td>\n",
  229. " <td>106.257402</td>\n",
  230. " </tr>\n",
  231. " <tr>\n",
  232. " <th>D</th>\n",
  233. " <td>9401.879005</td>\n",
  234. " <td>100.945629</td>\n",
  235. " </tr>\n",
  236. " </tbody>\n",
  237. "</table>\n",
  238. "</div>"
  239. ],
  240. "text/plain": [
  241. " data1 data2\n",
  242. "key1 \n",
  243. "A 11284.702046 106.384940\n",
  244. "B 10984.659582 123.035298\n",
  245. "C 7933.949489 106.257402\n",
  246. "D 9401.879005 100.945629"
  247. ]
  248. },
  249. "execution_count": 6,
  250. "metadata": {},
  251. "output_type": "execute_result"
  252. }
  253. ],
  254. "source": [
  255. "# データフレームの各列に対して key1 の平均値を算出\n",
  256. "df.groupby('key1').mean()"
  257. ]
  258. },
  259. {
  260. "cell_type": "code",
  261. "execution_count": 7,
  262. "metadata": {},
  263. "outputs": [
  264. {
  265. "data": {
  266. "text/html": [
  267. "<div>\n",
  268. "<style scoped>\n",
  269. " .dataframe tbody tr th:only-of-type {\n",
  270. " vertical-align: middle;\n",
  271. " }\n",
  272. "\n",
  273. " .dataframe tbody tr th {\n",
  274. " vertical-align: top;\n",
  275. " }\n",
  276. "\n",
  277. " .dataframe thead th {\n",
  278. " text-align: right;\n",
  279. " }\n",
  280. "</style>\n",
  281. "<table border=\"1\" class=\"dataframe\">\n",
  282. " <thead>\n",
  283. " <tr style=\"text-align: right;\">\n",
  284. " <th></th>\n",
  285. " <th>data1</th>\n",
  286. " <th>data2</th>\n",
  287. " </tr>\n",
  288. " <tr>\n",
  289. " <th>key2</th>\n",
  290. " <th></th>\n",
  291. " <th></th>\n",
  292. " </tr>\n",
  293. " </thead>\n",
  294. " <tbody>\n",
  295. " <tr>\n",
  296. " <th>1Gr</th>\n",
  297. " <td>11084.673737</td>\n",
  298. " <td>117.485179</td>\n",
  299. " </tr>\n",
  300. " <tr>\n",
  301. " <th>2Gr</th>\n",
  302. " <td>8423.259328</td>\n",
  303. " <td>104.486811</td>\n",
  304. " </tr>\n",
  305. " </tbody>\n",
  306. "</table>\n",
  307. "</div>"
  308. ],
  309. "text/plain": [
  310. " data1 data2\n",
  311. "key2 \n",
  312. "1Gr 11084.673737 117.485179\n",
  313. "2Gr 8423.259328 104.486811"
  314. ]
  315. },
  316. "execution_count": 7,
  317. "metadata": {},
  318. "output_type": "execute_result"
  319. }
  320. ],
  321. "source": [
  322. "# データフレームの各列に対して key2 の平均値を算出\n",
  323. "df.groupby('key2').mean()"
  324. ]
  325. }
  326. ],
  327. "metadata": {
  328. "kernelspec": {
  329. "display_name": "Python 3",
  330. "language": "python",
  331. "name": "python3"
  332. },
  333. "language_info": {
  334. "codemirror_mode": {
  335. "name": "ipython",
  336. "version": 3
  337. },
  338. "file_extension": ".py",
  339. "mimetype": "text/x-python",
  340. "name": "python",
  341. "nbconvert_exporter": "python",
  342. "pygments_lexer": "ipython3",
  343. "version": "3.7.1"
  344. }
  345. },
  346. "nbformat": 4,
  347. "nbformat_minor": 2
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement