Pandas in Python for Economics and Finance

SAFE Research Data Center

Errikos Melissinos, Research Assistant - May 2022

Logo SAFE

Abstract & Documentation:
In this Webinar we will give a basic introduction into Python’s Pandas libraries.
We will go through a series of applications covering topics in economics, finance and econometrics, while also utilising databases that are available to Goethe University.
*.ipynb file Please note that .ipynb file requires a Jupyter environment, for ex. Google Colab, Jupyter lab,
YouTube video (75 mins). Sections:


Pandas is a Python library that allows the manipulation of data through Dataframes.

Very basics of Python

Python is a programming language. The main advantages of Python:

Python has variables:
Python has conditional statements:
Python has for loops

It is also object-oriented. Everything can have its own methods and properties.

Python has functions

Now Pandas!

Python has many important libraries but here we will mostly look at Pandas.

First we will import the library so that we can use it in our Python session:

The way to start with Pandas is either my creating a Dataframe using data that has already been loaded, by reading an external file or by downloading the data.

Take a look at the data

How to get to the data.

In order to work with the data easier, I would like for the dates to appear as different rows. So, I need to reshape the data, pd.pivot() and pd.stack() can be used for such operations. However for this case pd.melt() fits my use case better.

Functions on columns

We can also apply our own function on the columns.

Rename, Create and Transform Columns

We can also transform the data by changing their data type.

Combining dataframes

Calculations by groups

Save data to external file

Run a regression

Compute the Fama French Factors

This code is taken from with some extra comments added by me: https://wrds-www.wharton.upenn.edu/pages/support/applications/python-replications/fama-french-factors-python/ The original paper can be found here: https://rady.ucsd.edu/faculty/directory/valkanov/pub/classes/mfe/docs/fama_french_jfe_1993.pdf (p. 8 for factors)