Introduction to Python and Economics

Python can be a powerful tool to help with Economics and Econometric analysis. To get started, you probably want to use Repl to run code, which is a simple way to get started with Python online. If you’re wanting to go beyond the basics, download both Python (the language) and an interface to write your code in, such as Atom. You can then run your code from within Atom or in command prompt. On this blog I will post code snippits within the text, with full reproducible documents on Repl – but feel free to use which method suits you. I would, however, recommend downloading python on your own device, as Repl may have issues using some of the packages I use below.

All of the following can be found on my Repl post, but I will spend the following time talking through it. Let’s get started by printing something – your first Python code!

# Example 1
print("Hello, World!")

Python can be used to run calculations:

# Example 2
print(1+2)

Alternatively, you could store the calculation for use later, but also print it out. Note that the number is stored as a variable called ‘number’, but we could call it anything, as long as the name is consistent throughout the file.

# Example 3
number = 1+2
print(number)

Python can be used to store lists of data, either integers, floats (numbers with decimals), or strings (text, written using ” “).

# Example 4
list_of_things = [1,2,"a",6.4,9]
print(list_of_things)

Let’s step things up a bit. In Time Series Econometrics at UKC in 3rd year we use the following dataset to conduct some analysis:

It’s an Excel file containing some financial data – remember to store the data in the same folder as your Python file! To use this data in Python we need some important packages which can enable us to import and manipulate the data. These are numpy and pandas. We can abbreviate these as np and pd. Make sure you have these both installed using:

pip install numpy
pip install pandas

We want to import the data and call it df, ignoring the first 10 rows of preamble. These packages will also help to rename the columns, and tell Python that the index is the list of dates in the date column. Let’s also print out the data to take a look.

# Example 5
#pip install xlrd # Installing dependancy
import pandas as pd # Importing pandas package
import numpy as np # Importing numpy package
df = pd.read_excel('PAYEMS21.xls', header=10) # Loading data
# Renaming and Indexing
df = df.rename(columns={'observation_date': 'date', 'PAYEMS': 'values'}) # Renaming columns
df.set_index("date",inplace=True) # Setting Index
print(df)
            values
date
1939-01-01   29923
1939-02-01   30100
1939-03-01   30280
1939-04-01   30094
1939-05-01   30299
...            ...
2020-09-01  141865
2020-10-01  142545
2020-11-01  142809
2020-12-01  142582
2021-01-01  142631

[985 rows x 1 columns]

As can be seen, we have time-series data, for the first day of each month, from 1939 to the start of 2021. Let’s plot the data using pyplot, and set the title. Notice the substantial drop in 2020 from the effect of lockdown on the economy.

# Example 6
from matplotlib import pyplot # Importing the relevant package
pyplot.title("Simple plot of the raw values") # Creating a title
df['values'].plot() # Creating the plot
pyplot.show() # Showing the plot

Finally, we could do with restricting the sample to the less volatile periods. We need to use the datetime package, and this is done by specifying the start date and end dates, separated by a colon. Let’s also print the dataframe out to check it’s worked.

# Example 7
import datetime
df = df.loc[datetime.date(year=1962,month=1,day=1):datetime.date(year=2019,month=12,day=31)] #Sample restriction
print(df)
            values
date
1962-01-01   54891
1962-02-01   55188
1962-03-01   55275
1962-04-01   55602
1962-05-01   55628
...            ...
2019-08-01  151108
2019-09-01  151329
2019-10-01  151524
2019-11-01  151758
2019-12-01  151919

[696 rows x 1 columns]

As can be seen, it has worked. That’s everying for now! In this blog post we have imported and formatted data, introduced a sample restriction, and printed out a summary. Be sure to check out future blog posts as we progess onto more advanced topics.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: