Data Science in Python — Part 2: Exploring Pokemon Data using Pandas and Seaborn
By: Chris Thai
Tags: Data Visualization, Pandas, Data Science, Matplotlib, Seaborn

Exploring the Pokemon dataset with Pandas

If you haven’t already, make sure to download the pokemon.csv file from Kaggle. Then, create a new Python script in the same directory (or notebook. Jupyter notebooks are very useful for tasks like these, but I won’t go over them in this series. You can find info about them here).

Let’s start by importing our csv file into a Pandas DataFrame. Pandas provides a very convenient method to directly import such files.

# import our dataset

pokemon_df = pd.read_csv('./pokemon.csv')

When we display the DataFrame, it’ll look something like this (I am using a Jupyter notebook by the way. Medium code inlines aren’t wide enough to make our DataFrames look pretty 😦):

Our unaltered DataFrame after importing

Filtering your DataFrame

One cool thing we can do is look for a specific Pokemon. We can query our Dataframe like this:

This returns to me a DataFrame with the single row that represents Bulbasaur. Let’s break down the statement pokemon_df[pokemon_df['Name']=='Bulbasaur']

The first mention of pokemon_df refers to the DataFrame itself, it’s the object we’re trying to access

refers to the DataFrame itself, it’s the object we’re trying to access Whatever’s going inside the [] is our ‘condition’. Basically, we want all rows that satisfy this specific condition

is our ‘condition’. Basically, we want all rows that satisfy this specific condition That condition is pokemon_df['Name']=='Bulbasaur' . This refers to any row who’s Name field is the value ‘Bulbasaur’.

You can use this syntax to filter your dataset further. I could get back all Pokemon who are legendary:

I could get all Pokemon who are primary Electric types:

I can even combine conditions and say, get all Pokemon who have both Defense and Sp. Def above 100. These are after all boolean statements (just make sure to add parentheses to your conditions).

Being able to filter your dataset is a very powerful capability; there are a wide variety of applications for this. You can inspect subgroups of your data, determine outliers, and so much more.

Descriptive statistics with Pandas

Descriptive statistics help describe, show, or summarize data in a meaningful way (as opposed to inferential statistics, which we may cover in a later article). These are your statistics like mean, median, standard deviation, etc.

Presenting data in it’s raw form (i.e. a huge csv file) is not very meaningful, so we present summary statistics to paint a brief picture of what our data looks like, and provide a simple interpretation. There are generally two types of stats used to describe data:

Measures of central tendency: these describe a “central position” of your data. These is your mean, mode, or median, depending on what you want to use. Measures of spread: these describe how spread out your data is (whether your data points are clumped together, or spread far apart). These include range, quartiles, standard deviation, and variance.

Pandas provides easy ways to display descriptive statistics. Here is a table of some of the descriptive statistics functions Pandas natively provides.

A list of descriptive statistics functions in Pandas

You can easily apply these functions to either your entire DataFrame or a single column.

Calling the mean on the entire DataFrame v/s a single column

Pandas also provides the describe() function, which provides all of the descriptive statistics you could possibly want of your data. It’s an extremely convenient, one-step method of getting the descriptive measures you need of your data. Just as before, you can call this on your entire DataFrame or a single column (I’ve also converted the values to ints to make the values easier on the eyes).

Using describe() on your DataFrame v/s a single column

Remember how you can filter your DataFrame? Well, you can get descriptive statistics out of filtered DataFrames too!

Filtering all Fighting type Pokemon and describing them

This is just one of the many cool uses of filtering your DataFrames — you can inspect and view descriptive statistics of subgroups of your data!

All right, we’ve learned how get things like the mean and variance using Pandas, but it seems like we’re missing something…………………….

A picture is worth a thousand words

Or in this case, a thousand numbers, quite literally. We are visual creatures — humans process visual data so much better than staring at a list of numbers.

Say we’re comparing legendary v/s non-legendary Pokemon. What would you rather look at to compare them, the two boxplots or the two lists of numbers? My money’s on the boxplots 😃.