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28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import bs4 as BeautifulSoup\n",
    "import pandas as pd\n",
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "list_of_urls = [\n",
    "    \"https://en.wikipedia.org/wiki/List_of_generation_I_Pok%C3%A9mon\",\n",
    "    \"https://en.wikipedia.org/wiki/List_of_generation_II_Pok%C3%A9mon\",\n",
    "    \"https://en.wikipedia.org/wiki/List_of_generation_III_Pok%C3%A9mon\",\n",
    "    \"https://en.wikipedia.org/wiki/List_of_generation_IV_Pok%C3%A9mon\",\n",
    "    \"https://en.wikipedia.org/wiki/List_of_generation_V_Pok%C3%A9mon\",\n",
    "    \"https://en.wikipedia.org/wiki/List_of_generation_VI_Pok%C3%A9mon\",\n",
    "    \"https://en.wikipedia.org/wiki/List_of_generation_VII_Pok%C3%A9mon\",\n",
    "    \"https://en.wikipedia.org/wiki/List_of_generation_VIII_Pok%C3%A9mon\",\n",
    "    \"https://en.wikipedia.org/wiki/List_of_generation_IX_Pok%C3%A9mon\",\n",
    "]\n",
    "\n",
    "generations = [\"I\", \"II\", \"III\", \"IV\", \"V\", \"VI\", \"VII\", \"VIII\", \"IX\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I\n",
      "II\n",
      "III\n",
      "IV\n",
      "V\n",
      "VI\n",
      "VII\n",
      "VIII\n",
      "IX\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "generation_list = []\n",
    "for generation, url in zip(generations, list_of_urls):\n",
    "    print(generation)\n",
    "    generation_list.append(pd.read_html(url))\n",
    "    time.sleep(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "biggest_table_from_each_url = []\n",
    "\n",
    "for tables in generation_list[:-1]:\n",
    "    largest_in_generation = tables[0]\n",
    "    for table in tables:\n",
    "        if table.size > largest_in_generation.size:\n",
    "            largest_in_generation = table\n",
    "\n",
    "    biggest_table_from_each_url.append(largest_in_generation)\n",
    "\n",
    "biggest_table_from_each_url.append(\n",
    "    generation_list[-1][1]\n",
    ")  # hacky, gen IX has fewer new pokemon than the table at the bottom of the wikipedia page."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "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",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">Name</th>\n",
       "      <th>National Pokédexnumber</th>\n",
       "      <th colspan=\"2\" halign=\"left\">Type(s)</th>\n",
       "      <th>Evolves from</th>\n",
       "      <th>Evolves into</th>\n",
       "      <th>Notes</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>English</th>\n",
       "      <th>Japanese</th>\n",
       "      <th>National Pokédexnumber</th>\n",
       "      <th>Primary</th>\n",
       "      <th>Secondary</th>\n",
       "      <th>Evolves from</th>\n",
       "      <th>Evolves into</th>\n",
       "      <th>Notes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Sprigatito</td>\n",
       "      <td>Nyaoha (ニャオハ)</td>\n",
       "      <td>TBA</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Beginning of evolution</td>\n",
       "      <td>Unknown</td>\n",
       "      <td>Sprigatito is a cat-like Pokémon and the Grass...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Fuecoco</td>\n",
       "      <td>Hogēta (ホゲータ)</td>\n",
       "      <td>TBA</td>\n",
       "      <td>Fire</td>\n",
       "      <td>Fire</td>\n",
       "      <td>Beginning of evolution</td>\n",
       "      <td>Unknown</td>\n",
       "      <td>Fuecoco is a crocodile-like Pokémon and the Fi...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Quaxly</td>\n",
       "      <td>Kuwassu (クワッス)</td>\n",
       "      <td>TBA</td>\n",
       "      <td>Water</td>\n",
       "      <td>Water</td>\n",
       "      <td>Beginning of evolution</td>\n",
       "      <td>Unknown</td>\n",
       "      <td>Quaxly is a duck-like Pokémon and the Water-ty...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Name                 National Pokédexnumber Type(s)            \\\n",
       "      English        Japanese National Pokédexnumber Primary Secondary   \n",
       "0  Sprigatito   Nyaoha (ニャオハ)                    TBA   Grass     Grass   \n",
       "1     Fuecoco   Hogēta (ホゲータ)                    TBA    Fire      Fire   \n",
       "2      Quaxly  Kuwassu (クワッス)                    TBA   Water     Water   \n",
       "\n",
       "             Evolves from Evolves into  \\\n",
       "             Evolves from Evolves into   \n",
       "0  Beginning of evolution      Unknown   \n",
       "1  Beginning of evolution      Unknown   \n",
       "2  Beginning of evolution      Unknown   \n",
       "\n",
       "                                               Notes  \n",
       "                                               Notes  \n",
       "0  Sprigatito is a cat-like Pokémon and the Grass...  \n",
       "1  Fuecoco is a crocodile-like Pokémon and the Fi...  \n",
       "2  Quaxly is a duck-like Pokémon and the Water-ty...  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "biggest_table_from_each_url[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(biggest_table_from_each_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(152, 8),\n",
       " (100, 8),\n",
       " (138, 8),\n",
       " (117, 8),\n",
       " (158, 8),\n",
       " (73, 8),\n",
       " (94, 8),\n",
       " (101, 8),\n",
       " (3, 8)]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[i.shape for i in biggest_table_from_each_url]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_pkmn = pd.concat(biggest_table_from_each_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultiIndex([(                  'Name',                'English'),\n",
       "            (                  'Name',               'Japanese'),\n",
       "            ('National Pokédexnumber', 'National Pokédexnumber'),\n",
       "            (               'Type(s)',                'Primary'),\n",
       "            (               'Type(s)',              'Secondary'),\n",
       "            (          'Evolves from',           'Evolves from'),\n",
       "            (          'Evolves into',           'Evolves into'),\n",
       "            (                 'Notes',                  'Notes')],\n",
       "           )"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all_pkmn.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_pkmn = (\n",
    "    all_pkmn.T.reset_index()\n",
    "    .drop(columns=[\"level_0\"])\n",
    "    .set_index(\"level_1\")\n",
    "    .T.rename(\n",
    "        columns={\n",
    "            \"English\": \"english_name\",\n",
    "            \"Japanese\": \"japanese_name\",\n",
    "            \"Primary\": \"primary_type\",\n",
    "            \"Secondary\": \"secondary_type\",\n",
    "        }\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = all_pkmn[[\"english_name\", \"primary_type\", \"secondary_type\", \"Notes\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def select_text_before_first_bracket(string):\n",
    "    return string.split(\"[\")[0]\n",
    "\n",
    "\n",
    "all_pkmn[\"primary_type\"] = all_pkmn[\"primary_type\"].apply(select_text_before_first_bracket)\n",
    "all_pkmn[\"secondary_type\"] = all_pkmn[\"secondary_type\"].apply(select_text_before_first_bracket)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Water       126\n",
       "Normal      111\n",
       "Grass        89\n",
       "Bug          80\n",
       "Psychic      64\n",
       "Fire         61\n",
       "Electric     55\n",
       "Rock         50\n",
       "Fighting     38\n",
       "Dark         37\n",
       "Ground       36\n",
       "Poison       36\n",
       "Ghost        32\n",
       "Dragon       31\n",
       "Steel        30\n",
       "Ice          29\n",
       "Fairy        23\n",
       "Flying        7\n",
       "Bird          1\n",
       "Name: primary_type, dtype: int64"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Wikipedia adds a number of footnotes to the types, this output is much cleaner.\n",
    "all_pkmn[\"primary_type\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "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>level_1</th>\n",
       "      <th>english_name</th>\n",
       "      <th>japanese_name</th>\n",
       "      <th>National Pokédexnumber</th>\n",
       "      <th>primary_type</th>\n",
       "      <th>secondary_type</th>\n",
       "      <th>Evolves from</th>\n",
       "      <th>Evolves into</th>\n",
       "      <th>Notes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>151</th>\n",
       "      <td>MissingNo.</td>\n",
       "      <td>Ketsuban (けつばん)</td>\n",
       "      <td>None[nb 8]</td>\n",
       "      <td>Bird</td>\n",
       "      <td>Normal</td>\n",
       "      <td>No evolution</td>\n",
       "      <td>No evolution</td>\n",
       "      <td>An error handler species, \"Missing Number\" was...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "level_1 english_name    japanese_name National Pokédexnumber primary_type  \\\n",
       "151       MissingNo.  Ketsuban (けつばん)             None[nb 8]         Bird   \n",
       "\n",
       "level_1 secondary_type  Evolves from  Evolves into  \\\n",
       "151             Normal  No evolution  No evolution   \n",
       "\n",
       "level_1                                              Notes  \n",
       "151      An error handler species, \"Missing Number\" was...  "
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Bird Type? Is that like flying or something?\n",
    "all_pkmn[all_pkmn.primary_type == \"Bird\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_pkmn.to_csv(\"pokemon.csv\")"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "45e1260056979d5382785f386f12ee00f44622d9a136ee7663e9a61a67ca2a68"
  },
  "kernelspec": {
   "display_name": "Python 3.10.0 ('projects-vBrzsZbN-py3.10')",
   "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.10.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}