Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dschep/executive-orders-notebook
A Jupyter Notebook to play with how many Executive Orders Presidents have signed
https://github.com/dschep/executive-orders-notebook
data-visualization jupyter jupyter-notebook trump
Last synced: about 2 months ago
JSON representation
A Jupyter Notebook to play with how many Executive Orders Presidents have signed
- Host: GitHub
- URL: https://github.com/dschep/executive-orders-notebook
- Owner: dschep
- Created: 2017-02-04T17:20:17.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-04-25T20:05:12.000Z (over 6 years ago)
- Last Synced: 2024-10-13T02:08:36.915Z (3 months ago)
- Topics: data-visualization, jupyter, jupyter-notebook, trump
- Language: Jupyter Notebook
- Size: 781 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Executive Order plots Jupyter notebook
I wanted to compare how many Executive Orders Trump is signing compared to recent previous presidents.
```python
from datetime import datetimeimport requests
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
from matplotlib import stylestyle.use('ggplot')
``````python
URL_TEMPLATE = 'http://www.presidency.ucsb.edu/executive_orders.php?Submit=DISPLAY&year={year}'
def get_executive_orders(start_year=1826):
for year in range(start_year, datetime.now().year + 1):
resp = requests.get(URL_TEMPLATE.format(year=year))
soup = BeautifulSoup(resp.content, 'html.parser')
table = soup.form.next_sibling.next_sibling
for row in table.find_all('tr')[1:]:
columns = row.find_all('td')
try:
date = datetime.strptime(columns[1].text, '%B %d, %Y')
except ValueError:
# for some reason one of the dates is June 0 ¯\_(ツ)_/¯
if columns[1].text == 'June 0, 1887':
date = datetime(1887, 6, 1)
else:
print('skipping unparsable date {}'.format(columns[1].text))
continue
yield {
'president': columns[0].text,
'date': date,
'name': columns[2].text,
'link': columns[2].a['href'],
}
```Simplify by only using presidents after 1937 when inaugurations are on Jan 20th and simplify even more to avoid issues with impeachment(Nixon) and assassinations(JFK) by skipping to 1977 (Jimmy Carter)
```python
orders_by_president_by_day_of_presidency = {}
inauguration_dates = {}
for order in get_executive_orders(1977):
if order['date'] < datetime(1977, 1, 20):
continue
if order['president'] not in inauguration_dates:
inauguration_dates[order['president']] = order['date'].replace(day=20)
start_date = inauguration_dates[order['president']]
orders = orders_by_president_by_day_of_presidency.setdefault(order['president'], {})
order['day'] = int((order['date'] - start_date).total_seconds() / 60 / 60 / 24)
if order['day'] not in orders:
orders[order['day']] = 1
else:
orders[order['day']] += 1
``````python
for president, orders in sorted(orders_by_president_by_day_of_presidency.items(),
key=lambda x: inauguration_dates[x[0]]):
x = range(max(orders) + 1)
y = []
total = 0
for i in x:
total += orders.get(i, 0)
y.append(total)
plt.plot(x, y, label=president, drawstyle='steps-post')plt.legend()
plt.title('Executive Orders over entire Term')
plt.xlabel('days in office')
plt.ylabel('number of executive orders')
plt.show()
```![png](README_files/README_5_0.png)
```python
days = (datetime.now() - inauguration_dates['Donald J. Trump']).total_seconds() / 60 / 60 / 24
max_ = 0
for president, orders in sorted(orders_by_president_by_day_of_presidency.items(),
key=lambda x: inauguration_dates[x[0]]):
if max(orders) > max_:
max_ = max(orders)
x = range(max_ + 1)[:int(days)]
y = []
total = 0
for i in x:
total += orders.get(i, 0)
y.append(total)
plt.plot(x, y, label=president, drawstyle='steps-post')plt.legend()
plt.title("Executive Orders compared to Trump's term so far")
plt.xlabel('days in office')
plt.ylabel('number of executive orders')
plt.show()
```![png](README_files/README_6_0.png)
And out of curiosity, how do those spikes at the ends of terms compare
```python
for president, orders in sorted(orders_by_president_by_day_of_presidency.items(),
key=lambda x: inauguration_dates[x[0]]):
if president == 'Donald J. Trump':
continue # skip Trump, his term hasn't ended yet :'(
last_day = max(orders)
y = []
total = 0
for i in range(last_day - 180, last_day + 1):
total += orders.get(i, 0)
y.append(total)
plt.plot(range(181), y, label=president, drawstyle='steps-post')plt.legend()
plt.title('Executive Orders in last month in Office')
plt.xlabel('days in office')
plt.ylabel('number of executive orders')
plt.show()
```![png](README_files/README_8_0.png)