Build a Pokedex in Python

In this tutorial, we will learn how to build a simple Pokedex using Python. Pokemon has a tremendous fan following and is still really popular all around the world.

In that show, we have often wondered about the special device called Pokedex which contains information about all the pokemon. All we fans have always wanted that special device along with all the other pokemon world fantasies, so let’s make a simple version of it.

Pokedex in Python

Before starting with the code we should get the datasets needed for our Pokedex. I took datasets from two sources
https://www.kaggle.com/davidrgp/pokedex  – pokemon details
https://www.kaggle.com/kvpratama/pokemon-images-dataset/  – pokemon images

But as both image and detail datasets were from different sources so editing was required in both. I have uploaded the edited datasets for both image and details for you guys : )

Required Datasets – Download This

So first download the dataset from the link given above (you can even use some other datasets just make sure they are in proper order).

Let’s begin our code first we will be importing all the necessary libraries required and then we will be importing our datasets.

import pandas as pd
import matplotlib.pyplot as plt
import os
import glob
import natsort 
#reading the images dataset
dir1 = r"C:\Users\yash1\Desktop\pokedex\pokemon_images"  

path1 = os.path.join(dir1,'*g')

files = glob.glob(path1)
files1= natsort.natsorted(files,reverse=False)

imag = []
for x in files1:
    img = plt.imread(x)
    imag.append(img)
    
#reading the details dataset
data = pd.read_csv('pokemon.csv')

I hope you all are familiar with all the above libraries except the natsort. Natsort library is used for natural sorting in Python, it will be more clear when I will explain the importing step.

First, we will be importing the image dataset, so first we will be assigning the path of the directory to a variable then use that to access all the files in the directory. Here we have used natsort in order to sort the files in ascending order of their name. We append each file (image) into an array (imag) and then later access this using matplotlib. After that, we upload the details dataset.

After uploading the dataset we will begin with our main coding part

print("Pokedex\n")
print("Welcome Pokemon Lovers\n")
print("Search for a pokemon\n")
df1 =input("<A>Search by pokemon name\n<B>Search by pokemon ID\n(select A or B)\n")
df1.upper()

Here we first welcome the user and then we provide them with two options either to search a pokemon by name or to search one using the special ID assigned to each. Depending on the choice of user we proceed.

Pokedex

Welcome Pokemon Lovers

Search for a pokemon


<A>Search by pokemon name
<B>Search by pokemon ID
(select A or B)




If the user wants to search using the name of the pokemon:

if(df1=="A"):
    print("Enter the name of the pokemon")
    name = input()
    name.lower().strip()
    dt = data[:].where(data['pokemon']==name)
    st = dt[dt['id'].notnull()]    

We ask the user name of the pokemon then store it in some variable. We make sure that the name string is in lower case and doesn’t have any whitespaces, we do this in order to make sure that in whatever form the user enters the name of the pokemon it will match an entry in the dataset given that the spelling should be correct.

After that, we match that variable with the ‘pokemon’ column of our dataset which we have imported in variable data and store it in another variable dt. Now dt contains only the entry or row whose pokemon column is equal to the name of the pokemon provided by the user and the rest of the entries are none. We remove the none entries using notnull(), by only allowing those entries to be stored in a separate variable st which are not null.

We also store the index of the matched row using :

idx = dt.index[dt['pokemon']==name]

Then we iterate over each column and print each column name and the value stored in it.

for i in st.columns:
    print(i," : ",st[i][idx[0]])

After that, we display the image of the pokemon using the index of the matched row we found above.

if idx>721:
    then exit(0)
plt.imshow(imag[idx[0]])
plt.axis("off")   # turns off axes
plt.axis("tight")  # gets rid of white border
plt.axis("image")  # square up the image instead of filling the "figure" space
plt.show()

We can follow the same steps when the user selects Search by ID.

elif(df1=="B"):
    print("Enter the ID of the pokemon")
    ID = int(input())
    tt = data[:].where(data['id']==ID)
    idx1 = tt.index[tt['id']==ID]
    qt = tt[tt['id'].notnull()]
    for i in qt.columns:
        print(i," : ",qt[i][idx1[0]])
    if idx1>721:
        then exit(0)
    plt.imshow(imag[idx1[0]])
    plt.axis("off")   # turns off axes
    plt.axis("tight")  # gets rid of white border
    plt.axis("image")  # square up the image instead of filling the "figure" space
    plt.show()

OUTPUT

Pokedex

Welcome Pokemon Lovers

Search for a pokemon


<A>Search by pokemon name
<B>Search by pokemon ID
(select A or B)
A
Enter the name of the pokemon

snorlax
id : 143.0
pokemon : snorlax
species_id : 143.0
height : 21.0
weight : 4600.0
base_experience : 189.0
type_1 : normal
type_2 : nan
attack : 110.0
defense : 65.0
hp : 160.0
special_attack : 65.0
special_defense : 110.0
speed : 30.0
ability_1 : immunity
ability_2 : thick-fat
ability_hidden : gluttony color_1 : #A8A878
color_2 : nan
color_f : nan
egg_group_1 : monster
egg_group_2 : nan
url_image : 143.png
generation_id : 1.0
evolves_from_species_id : 446.0
evolution_chain_id : 72.0
shape_id : 12.0
shape : humanoid

 

It will also be displaying an image corresponding to the result.

Also learn:

Leave a Reply

Your email address will not be published. Required fields are marked *