---
jupytext:
text_representation:
extension: .md
format_name: myst
kernelspec:
display_name: Python 3
language: python
name: python3
---
(pd)=
```{raw} jupyter
```
# {index}`Pandas `
```{index} single: Python; Pandas
```
```{contents} Contents
:depth: 2
```
In addition to what’s in Anaconda, this lecture will need the following libraries:
```{code-cell} ipython
---
tags: [hide-output]
---
!pip install --upgrade pandas-datareader
!pip install --upgrade yfinance
```
## Overview
[Pandas](http://pandas.pydata.org/) is a package of fast, efficient data analysis tools for Python.
Its popularity has surged in recent years, coincident with the rise
of fields such as data science and machine learning.
Here's a popularity comparison over time against Matlab and STATA courtesy of Stack Overflow Trends
```{figure} /_static/lecture_specific/pandas/pandas_vs_rest.png
:scale: 100
```
Just as [NumPy](http://www.numpy.org/) provides the basic array data type plus core array operations, pandas
1. defines fundamental structures for working with data and
1. endows them with methods that facilitate operations such as
* reading in data
* adjusting indices
* working with dates and time series
* sorting, grouping, re-ordering and general data munging [^mung]
* dealing with missing values, etc., etc.
More sophisticated statistical functionality is left to other packages, such
as [statsmodels](http://www.statsmodels.org/) and [scikit-learn](http://scikit-learn.org/), which are built on top of pandas.
This lecture will provide a basic introduction to pandas.
Throughout the lecture, we will assume that the following imports have taken
place
```{code-cell} ipython
%matplotlib inline
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [10,8] # Set default figure size
import requests
```
Two important data types defined by pandas are `Series` and `DataFrame`.
You can think of a `Series` as a "column" of data, such as a collection of observations on a single variable.
A `DataFrame` is a two-dimensional object for storing related columns of data.
## Series
```{index} single: Pandas; Series
```
Let's start with Series.
We begin by creating a series of four random observations
```{code-cell} python3
s = pd.Series(np.random.randn(4), name='daily returns')
s
```
Here you can imagine the indices `0, 1, 2, 3` as indexing four listed
companies, and the values being daily returns on their shares.
Pandas `Series` are built on top of NumPy arrays and support many similar
operations
```{code-cell} python3
s * 100
```
```{code-cell} python3
np.abs(s)
```
But `Series` provide more than NumPy arrays.
Not only do they have some additional (statistically oriented) methods
```{code-cell} python3
s.describe()
```
But their indices are more flexible
```{code-cell} python3
s.index = ['AMZN', 'AAPL', 'MSFT', 'GOOG']
s
```
Viewed in this way, `Series` are like fast, efficient Python dictionaries
(with the restriction that the items in the dictionary all have the same
type---in this case, floats).
In fact, you can use much of the same syntax as Python dictionaries
```{code-cell} python3
s['AMZN']
```
```{code-cell} python3
s['AMZN'] = 0
s
```
```{code-cell} python3
'AAPL' in s
```
## DataFrames
```{index} single: Pandas; DataFrames
```
While a `Series` is a single column of data, a `DataFrame` is several columns, one for each variable.
In essence, a `DataFrame` in pandas is analogous to a (highly optimized) Excel spreadsheet.
Thus, it is a powerful tool for representing and analyzing data that are naturally organized into rows and columns, often with descriptive indexes for individual rows and individual columns.
Let's look at an example that reads data from the CSV file `pandas/data/test_pwt.csv`, which is taken from the [Penn World Tables](https://www.rug.nl/ggdc/productivity/pwt/pwt-releases/pwt-7.0).
The dataset contains the following indicators
| Variable Name | Description |
| :-: | :-: |
| POP | Population (in thousands) |
| XRAT | Exchange Rate to US Dollar |
| tcgdp | Total PPP Converted GDP (in million international dollar) |
| cc | Consumption Share of PPP Converted GDP Per Capita (%) |
| cg | Government Consumption Share of PPP Converted GDP Per Capita (%) |
We'll read this in from a URL using the `pandas` function `read_csv`.
```{code-cell} python3
df = pd.read_csv('https://raw.githubusercontent.com/QuantEcon/lecture-python-programming/master/source/_static/lecture_specific/pandas/data/test_pwt.csv')
type(df)
```
Here's the content of `test_pwt.csv`
```{code-cell} python3
df
```
### Select Data by Position
In practice, one thing that we do all the time is to find, select and work with a subset of the data of our interests.
We can select particular rows using standard Python array slicing notation
```{code-cell} python3
df[2:5]
```
To select columns, we can pass a list containing the names of the desired columns represented as strings
```{code-cell} python3
df[['country', 'tcgdp']]
```
To select both rows and columns using integers, the `iloc` attribute should be used with the format `.iloc[rows, columns]`.
```{code-cell} python3
df.iloc[2:5, 0:4]
```
To select rows and columns using a mixture of integers and labels, the `loc` attribute can be used in a similar way
```{code-cell} python3
df.loc[df.index[2:5], ['country', 'tcgdp']]
```
### Select Data by Conditions
Instead of indexing rows and columns using integers and names, we can also obtain a sub-dataframe of our interests that satisfies certain (potentially complicated) conditions.
This section demonstrates various ways to do that.
The most straightforward way is with the `[]` operator.
```{code-cell} python3
df[df.POP >= 20000]
```
To understand what is going on here, notice that `df.POP >= 20000` returns a series of boolean values.
```{code-cell} python3
df.POP >= 20000
```
In this case, `df[___]` takes a series of boolean values and only returns rows with the `True` values.
Take one more example,
```{code-cell} python3
df[(df.country.isin(['Argentina', 'India', 'South Africa'])) & (df.POP > 40000)]
```
However, there is another way of doing the same thing, which can be slightly faster for large dataframes, with more natural syntax.
```{code-cell} python3
# the above is equivalent to
df.query("POP >= 20000")
```
```{code-cell} python3
df.query("country in ['Argentina', 'India', 'South Africa'] and POP > 40000")
```
We can also allow arithmetic operations between different columns.
```{code-cell} python3
df[(df.cc + df.cg >= 80) & (df.POP <= 20000)]
```
```{code-cell} python3
# the above is equivalent to
df.query("cc + cg >= 80 & POP <= 20000")
```
For example, we can use the conditioning to select the country with the largest household consumption - gdp share `cc`.
```{code-cell} python3
df.loc[df.cc == max(df.cc)]
```
When we only want to look at certain columns of a selected sub-dataframe, we can use the above conditions with the `.loc[__ , __]` command.
The first argument takes the condition, while the second argument takes a list of columns we want to return.
```{code-cell} python3
df.loc[(df.cc + df.cg >= 80) & (df.POP <= 20000), ['country', 'year', 'POP']]
```
**Application: Subsetting Dataframe**
Real-world datasets can be [enormous](https://developers.google.com/machine-learning/data-prep/construct/collect/data-size-quality).
It is sometimes desirable to work with a subset of data to enhance computational efficiency and reduce redundancy.
Let's imagine that we're only interested in the population (`POP`) and total GDP (`tcgdp`).
One way to strip the data frame `df` down to only these variables is to overwrite the dataframe using the selection method described above
```{code-cell} python3
df_subset = df[['country', 'POP', 'tcgdp']]
df_subset
```
We can then save the smaller dataset for further analysis.
```{code-block} python3
:class: no-execute
df_subset.to_csv('pwt_subset.csv', index=False)
```
### Apply Method
Another widely used Pandas method is `df.apply()`.
It applies a function to each row/column and returns a series.
This function can be some built-in functions like the `max` function, a `lambda` function, or a user-defined function.
Here is an example using the `max` function
```{code-cell} python3
df[['year', 'POP', 'XRAT', 'tcgdp', 'cc', 'cg']].apply(max)
```
This line of code applies the `max` function to all selected columns.
`lambda` function is often used with `df.apply()` method
A trivial example is to return itself for each row in the dataframe
```{code-cell} python3
df.apply(lambda row: row, axis=1)
```
```{note}
For the `.apply()` method
- axis = 0 -- apply function to each column (variables)
- axis = 1 -- apply function to each row (observations)
- axis = 0 is the default parameter
```
We can use it together with `.loc[]` to do some more advanced selection.
```{code-cell} python3
complexCondition = df.apply(
lambda row: row.POP > 40000 if row.country in ['Argentina', 'India', 'South Africa'] else row.POP < 20000,
axis=1), ['country', 'year', 'POP', 'XRAT', 'tcgdp']
```
`df.apply()` here returns a series of boolean values rows that satisfies the condition specified in the if-else statement.
In addition, it also defines a subset of variables of interest.
```{code-cell} python3
complexCondition
```
When we apply this condition to the dataframe, the result will be
```{code-cell} python3
df.loc[complexCondition]
```
### Make Changes in DataFrames
The ability to make changes in dataframes is important to generate a clean dataset for future analysis.
**1.** We can use `df.where()` conveniently to "keep" the rows we have selected and replace the rest rows with any other values
```{code-cell} python3
df.where(df.POP >= 20000, False)
```
**2.** We can simply use `.loc[]` to specify the column that we want to modify, and assign values
```{code-cell} python3
df.loc[df.cg == max(df.cg), 'cg'] = np.nan
df
```
**3.** We can use the `.apply()` method to modify *rows/columns as a whole*
```{code-cell} python3
def update_row(row):
# modify POP
row.POP = np.nan if row.POP<= 10000 else row.POP
# modify XRAT
row.XRAT = row.XRAT / 10
return row
df.apply(update_row, axis=1)
```
**4.** We can use the `.applymap()` method to modify all *individual entries* in the dataframe altogether.
```{code-cell} python3
# Round all decimal numbers to 2 decimal places
df.applymap(lambda x : round(x,2) if type(x)!=str else x)
```
**Application: Missing Value Imputation**
Replacing missing values is an important step in data munging.
Let's randomly insert some NaN values
```{code-cell} python3
for idx in list(zip([0, 3, 5, 6], [3, 4, 6, 2])):
df.iloc[idx] = np.nan
df
```
The `zip()` function here creates pairs of values from the two lists (i.e. [0,3], [3,4] ...)
We can use the `.applymap()` method again to replace all missing values with 0
```{code-cell} python3
# replace all NaN values by 0
def replace_nan(x):
if type(x)!=str:
return 0 if np.isnan(x) else x
else:
return x
df.applymap(replace_nan)
```
Pandas also provides us with convenient methods to replace missing values.
For example, single imputation using variable means can be easily done in pandas
```{code-cell} python3
df = df.fillna(df.iloc[:,2:8].mean())
df
```
Missing value imputation is a big area in data science involving various machine learning techniques.
There are also more [advanced tools](https://scikit-learn.org/stable/modules/impute.html) in python to impute missing values.
### Standardization and Visualization
Let's imagine that we're only interested in the population (`POP`) and total GDP (`tcgdp`).
One way to strip the data frame `df` down to only these variables is to overwrite the dataframe using the selection method described above
```{code-cell} python3
df = df[['country', 'POP', 'tcgdp']]
df
```
Here the index `0, 1,..., 7` is redundant because we can use the country names as an index.
To do this, we set the index to be the `country` variable in the dataframe
```{code-cell} python3
df = df.set_index('country')
df
```
Let's give the columns slightly better names
```{code-cell} python3
df.columns = 'population', 'total GDP'
df
```
The `population` variable is in thousands, let's revert to single units
```{code-cell} python3
df['population'] = df['population'] * 1e3
df
```
Next, we're going to add a column showing real GDP per capita, multiplying by 1,000,000 as we go because total GDP is in millions
```{code-cell} python3
df['GDP percap'] = df['total GDP'] * 1e6 / df['population']
df
```
One of the nice things about pandas `DataFrame` and `Series` objects is that they have methods for plotting and visualization that work through Matplotlib.
For example, we can easily generate a bar plot of GDP per capita
```{code-cell} python3
ax = df['GDP percap'].plot(kind='bar')
ax.set_xlabel('country', fontsize=12)
ax.set_ylabel('GDP per capita', fontsize=12)
plt.show()
```
At the moment the data frame is ordered alphabetically on the countries---let's change it to GDP per capita
```{code-cell} python3
df = df.sort_values(by='GDP percap', ascending=False)
df
```
Plotting as before now yields
```{code-cell} python3
ax = df['GDP percap'].plot(kind='bar')
ax.set_xlabel('country', fontsize=12)
ax.set_ylabel('GDP per capita', fontsize=12)
plt.show()
```
## On-Line Data Sources
```{index} single: Data Sources
```
Python makes it straightforward to query online databases programmatically.
An important database for economists is [FRED](https://research.stlouisfed.org/fred2/) --- a vast collection of time series data maintained by the St. Louis Fed.
For example, suppose that we are interested in the [unemployment rate](https://research.stlouisfed.org/fred2/series/UNRATE).
Via FRED, the entire series for the US civilian unemployment rate can be downloaded directly by entering
this URL into your browser (note that this requires an internet connection)
```{code-block} none
https://research.stlouisfed.org/fred2/series/UNRATE/downloaddata/UNRATE.csv
```
(Equivalently, click here: [https://research.stlouisfed.org/fred2/series/UNRATE/downloaddata/UNRATE.csv](https://research.stlouisfed.org/fred2/series/UNRATE/downloaddata/UNRATE.csv))
This request returns a CSV file, which will be handled by your default application for this class of files.
Alternatively, we can access the CSV file from within a Python program.
This can be done with a variety of methods.
We start with a relatively low-level method and then return to pandas.
### Accessing Data with {index}`requests `
```{index} single: Python; requests
```
One option is to use [requests](https://requests.readthedocs.io/en/master/), a standard Python library for requesting data over the Internet.
To begin, try the following code on your computer
```{code-cell} python3
r = requests.get('http://research.stlouisfed.org/fred2/series/UNRATE/downloaddata/UNRATE.csv')
```
If there's no error message, then the call has succeeded.
If you do get an error, then there are two likely causes
1. You are not connected to the Internet --- hopefully, this isn't the case.
1. Your machine is accessing the Internet through a proxy server, and Python isn't aware of this.
In the second case, you can either
* switch to another machine
* solve your proxy problem by reading [the documentation](https://requests.readthedocs.io/en/master/)
Assuming that all is working, you can now proceed to use the `source` object returned by the call `requests.get('http://research.stlouisfed.org/fred2/series/UNRATE/downloaddata/UNRATE.csv')`
```{code-cell} python3
url = 'http://research.stlouisfed.org/fred2/series/UNRATE/downloaddata/UNRATE.csv'
source = requests.get(url).content.decode().split("\n")
source[0]
```
```{code-cell} python3
source[1]
```
```{code-cell} python3
source[2]
```
We could now write some additional code to parse this text and store it as an array.
But this is unnecessary --- pandas' `read_csv` function can handle the task for us.
We use `parse_dates=True` so that pandas recognizes our dates column, allowing for simple date filtering
```{code-cell} python3
data = pd.read_csv(url, index_col=0, parse_dates=True)
```
The data has been read into a pandas DataFrame called `data` that we can now manipulate in the usual way
```{code-cell} python3
type(data)
```
```{code-cell} python3
data.head() # A useful method to get a quick look at a data frame
```
```{code-cell} python3
pd.set_option('display.precision', 1)
data.describe() # Your output might differ slightly
```
We can also plot the unemployment rate from 2006 to 2012 as follows
```{code-cell} python3
ax = data['2006':'2012'].plot(title='US Unemployment Rate', legend=False)
ax.set_xlabel('year', fontsize=12)
ax.set_ylabel('%', fontsize=12)
plt.show()
```
Note that pandas offers many other file type alternatives.
Pandas has [a wide variety](https://pandas.pydata.org/pandas-docs/stable/user_guide/io.html) of top-level methods that we can use to read, excel, json, parquet or plug straight into a database server.
### Using {index}`pandas_datareader ` and {index}`yfinance ` to Access Data
```{index} single: Python; pandas-datareader
```
The maker of pandas has also authored a library called
[pandas_datareader](https://pandas-datareader.readthedocs.io/en/latest/) that
gives programmatic access to many data sources straight from the Jupyter notebook.
While some sources require an access key, many of the most important (e.g., FRED, [OECD](https://data.oecd.org/), [EUROSTAT](https://ec.europa.eu/eurostat/data/database) and the World Bank) are free to use.
We will also use [yfinance](https://pypi.org/project/yfinance/) to fetch data from Yahoo finance
in the exercises.
For now let's work through one example of downloading and plotting data --- this
time from the World Bank.
```{note}
There are also other [python libraries](https://data.worldbank.org/products/third-party-apps)
available for working with world bank data such as [wbgapi](https://pypi.org/project/wbgapi/)
```
The World Bank [collects and organizes data](http://data.worldbank.org/indicator) on a huge range of indicators.
For example, [here's](http://data.worldbank.org/indicator/GC.DOD.TOTL.GD.ZS/countries) some data on government debt as a ratio to GDP.
The next code example fetches the data for you and plots time series for the US and Australia
```{code-cell} python3
from pandas_datareader import wb
govt_debt = wb.download(indicator='GC.DOD.TOTL.GD.ZS', country=['US', 'AU'], start=2005, end=2016).stack().unstack(0)
ind = govt_debt.index.droplevel(-1)
govt_debt.index = ind
ax = govt_debt.plot(lw=2)
ax.set_xlabel('year', fontsize=12)
plt.title("Government Debt to GDP (%)")
plt.show()
```
The [documentation](https://pandas-datareader.readthedocs.io/en/latest/index.html) provides more details on how to access various data sources.
## Exercises
```{exercise-start}
:label: pd_ex1
```
With these imports:
```{code-cell} python3
import datetime as dt
import yfinance as yf
```
Write a program to calculate the percentage price change over 2021 for the following shares:
```{code-cell} python3
ticker_list = {'INTC': 'Intel',
'MSFT': 'Microsoft',
'IBM': 'IBM',
'BHP': 'BHP',
'TM': 'Toyota',
'AAPL': 'Apple',
'AMZN': 'Amazon',
'C': 'Citigroup',
'QCOM': 'Qualcomm',
'KO': 'Coca-Cola',
'GOOG': 'Google'}
```
Here's the first part of the program
```{code-cell} python3
def read_data(ticker_list,
start=dt.datetime(2021, 1, 1),
end=dt.datetime(2021, 12, 31)):
"""
This function reads in closing price data from Yahoo
for each tick in the ticker_list.
"""
ticker = pd.DataFrame()
for tick in ticker_list:
stock = yf.Ticker(tick)
prices = stock.history(start=start, end=end)
# Change the index to date-only
prices.index = pd.to_datetime(prices.index.date)
closing_prices = prices['Close']
ticker[tick] = closing_prices
return ticker
ticker = read_data(ticker_list)
```
Complete the program to plot the result as a bar graph like this one:
```{figure} /_static/lecture_specific/pandas/pandas_share_prices.png
:scale: 80
```
```{exercise-end}
```
```{solution-start} pd_ex1
:class: dropdown
```
There are a few ways to approach this problem using Pandas to calculate
the percentage change.
First, you can extract the data and perform the calculation such as:
```{code-cell} python3
p1 = ticker.iloc[0] #Get the first set of prices as a Series
p2 = ticker.iloc[-1] #Get the last set of prices as a Series
price_change = (p2 - p1) / p1 * 100
price_change
```
Alternatively you can use an inbuilt method `pct_change` and configure it to
perform the correct calculation using `periods` argument.
```{code-cell} python3
change = ticker.pct_change(periods=len(ticker)-1, axis='rows')*100
price_change = change.iloc[-1]
price_change
```
Then to plot the chart
```{code-cell} python3
price_change.sort_values(inplace=True)
price_change = price_change.rename(index=ticker_list)
fig, ax = plt.subplots(figsize=(10,8))
ax.set_xlabel('stock', fontsize=12)
ax.set_ylabel('percentage change in price', fontsize=12)
price_change.plot(kind='bar', ax=ax)
plt.show()
```
```{solution-end}
```
```{exercise-start}
:label: pd_ex2
```
Using the method `read_data` introduced in {ref}`pd_ex1`, write a program to obtain year-on-year percentage change for the following indices:
```{code-cell} python3
indices_list = {'^GSPC': 'S&P 500',
'^IXIC': 'NASDAQ',
'^DJI': 'Dow Jones',
'^N225': 'Nikkei'}
```
Complete the program to show summary statistics and plot the result as a time series graph like this one:
```{figure} /_static/lecture_specific/pandas/pandas_indices_pctchange.png
:scale: 80
```
```{exercise-end}
```
```{solution-start} pd_ex2
:class: dropdown
```
Following the work you did in {ref}`pd_ex1`, you can query the data using `read_data` by updating the start and end dates accordingly.
```{code-cell} python3
indices_data = read_data(
indices_list,
start=dt.datetime(1971, 1, 1), #Common Start Date
end=dt.datetime(2021, 12, 31)
)
```
Then, extract the first and last set of prices per year as DataFrames and calculate the yearly returns such as:
```{code-cell} python3
yearly_returns = pd.DataFrame()
for index, name in indices_list.items():
p1 = indices_data.groupby(indices_data.index.year)[index].first() # Get the first set of returns as a DataFrame
p2 = indices_data.groupby(indices_data.index.year)[index].last() # Get the last set of returns as a DataFrame
returns = (p2 - p1) / p1
yearly_returns[name] = returns
yearly_returns
```
Next, you can obtain summary statistics by using the method `describe`.
```{code-cell} python3
yearly_returns.describe()
```
Then, to plot the chart
```{code-cell} python3
fig, axes = plt.subplots(2, 2, figsize=(10, 8))
for iter_, ax in enumerate(axes.flatten()): # Flatten 2-D array to 1-D array
index_name = yearly_returns.columns[iter_] # Get index name per iteration
ax.plot(yearly_returns[index_name]) # Plot pct change of yearly returns per index
ax.set_ylabel("percent change", fontsize = 12)
ax.set_title(index_name)
plt.tight_layout()
```
```{solution-end}
```
[^mung]: Wikipedia defines munging as cleaning data from one raw form into a structured, purged one.