Python Pandas Tutorial

Pandas is useful for doing data analysis in Python. Today we will discuss how to install Pandas, some of the basic concepts of Pandas Dataframes, then some of the common Pandas use cases.

What is Pandas?

Pandas is a Python library for doing data analysis. Typically you will use it for working with 1-dimentional series data, or 2-dimentional data called data frames. This might include:

  • Tabular data like SQL tables or Excel spreadsheets
  • Ordered and unordered time series data.
  • Matrix data with row and column labels
  • Other observational / statistical data sets

We will get into more specific examples later on.

Pandas is built on top of Numpy, so you could think of it as an extension of that. For that reason, it is good to have an understanding on Numpy before proceeding. Although that is not a strict requirement.

What is Numpy?

Numpy is short for Numerical Python. It is a python library for working with multi-dimentional arrays and matrices. A few of the useful features of Numpy are:

  • Creating multi-dimentional arrays
  • Indexing and searching arrays
  • Array arithemtic.
  • Comparing arrays with different dimentions

Installing Pandas

The easiest way to install Pandas is to use Pip. You can runPip3 install Pandas- or -Pip install PandasThis will depend on if you are using Python 2 or Python 3. I recommend using Python 3 since Python 2 will be end of life in the next few years. If you don’t have Pip installed, checkout our article on Python Basics for instructions on how to install Pip.

As an alternative to using Pip you you can also install Anaconda. Anaconda includes a whole suite of datascience tools, one of which is Pandas. It also includes Numpy.

Using Pandas

For our examples, we are going to use the 2016 presidential polling data from The data can be found here. But we are going to code the URL into our code, so downloading it ahead of time is optional. Althought, your script will run faster if you download it ahead of time. Some of the more common use cases for Pandas are:

  • Filtering Data
  • Summarizing Data
  • Plotting Data
  • Pivoting Data

First thing to do is import our dependencies, and then read in our csv file with our data:


You could then run print(df) and see all of the data from the CSV. that is not that useful. We shuld focus in on the data we really care about. This dataset contains polling data from various surveys from all 50 states. Let’s pick a specific poll and a specific state:


Next, let’s plot the poll results of both Trump and Clinton:

Your result should look something like this:


That is useful. But it would be more helpful if we could add some labels
We can add the legend parameter to identify each line:

your chart should now look more like this:

Trump v Clinton plot


That looks even better. When we start going beyond this point, I think it is a lot easier to use matplotlib directly to do more plotting. Here is a similar plot done using matplotlib:

Here is the result:

As you can see above, we start by importing our libraries, then reading our csv file. We then sort our values based on the date of the poll, then we plot both the clinton and trump approval ratings. We add a legend by calling plt.legend(). We add the label on the left side of the graph using the plt.ylabel command. We then rotate the dates along the bottom by 45 degrees with the plt.xticks command. Finally we show our graph with the command.

When you do plotting, Pandas is just using matplotlib anyway. So what we have done is stepped back and done it outside of pandas. But it is still using the same libraries.


Pivoting data is when you take the columns and make them the rows and vice versa. It is a good way to get a diffrent persective on your data. And it is better than simply tilting your head to the left. We will use the same dataset as the previous section in our examples. Just like before, we will start by importing our libraries:

Next we read our CSV file and create our data frame:

Next we want to see what Registered Voters are saying vs Likely Voters in our samlples. So we are going to Pivot uding the population column as our column list:

Your output should look similar to this:

Using this pivot table you can see the approval ratings for Clinton among likely voters and registered voters. Those NaN’s get in the way, so let’s get the average of each column:

In the above command we added the .mean() function with the skipna=True option. This takes the average of each column, but skips all of the NaN values.

Your output should look similar to this:

Here is all of our pivot table code consolidated:


It can be taunting to look at a large dataset. However, Pandas gives you some nice tools for summarizing the data so you don’t have to try to take on the entire dataset at once.

To start, we have the min, max and median functions. These functions do as they say and return the minimum, maximum, and average values. You can see examples of each below using our Pivot Table from the previous section:

Next it might be helpful to know the number of unique values you have in a dataset:

Or if you just want a quick summary, you can use the describe function:


The output of the describe function is the most useful as it combines many of the previous functions we talked about. Your output will look similar to this:


%d bloggers like this: