In [1]:
import pandas as pd
import numpy as np
import requests
from lxml import html
from bs4 import BeautifulSoup
import os.path
from datetime import datetime, timedelta
import calendar
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
import re
In [2]:
pathEo='./eos.csv'
In [3]:
if os.path.isfile(pathEo):
os.remove(pathEo)
eoYear=2017
with open(pathEo, 'a') as f:
while (eoYear >= 1826):
if eoYear % 4 == 0:
print(eoYear)
eoPage = requests.get('http://www.presidency.ucsb.edu/executive_orders.php?year=' + str(eoYear)+ '&Submit=DISPLAY')
eoYear -= 1
eoSoup = BeautifulSoup(eoPage.text, 'lxml')
eoTable = eoSoup.find('table', width='700', border='0')
for row in eoTable.findAll('tr'):
tds = row.findAll('td')
if not 'president' in tds[0].text.lower():
f.write(tds[0].text + ',')
f.write('"' + tds[1].text + '",')
f.write('"' + tds[2].text.replace(',','') + '"\n')
In [5]:
dfEo = pd.read_csv(pathEo, encoding = "ISO-8859-1", index_col = False, names=['President', 'Date', 'Order'])
dfEo['Date'] = pd.to_datetime(dfEo['Date'], format = '%B %d, %Y')
dfEo['Year'] = dfEo['Date'].dt.year
dfEo['Month'] = dfEo['Date'].dt.month
dfEo['Day'] = dfEo['Date'].dt.day
dfEo.set_index('Date', inplace=True)
In [6]:
ct = pd.crosstab([dfEo.President, dfEo.Year, dfEo.Month], columns='Order')
ct.head()
#ct.xs('Abraham Lincoln', level='President')
Out[6]:
In [7]:
ctObama = ct.loc[ct.index.get_level_values('President')=='Barack Obama']
ctObama.head()
#ctObama.index
Out[7]:
In [8]:
eo_path = './Executive Orders/'
pres='Barack Obama'
for pres in dfEo.President.unique():
if not os.path.isfile(os.path.join(eo_path, pres.replace(' ', '-')+'.png')):
print(pres)
dateRange = pd.DatetimeIndex(start=dfEo[dfEo.President == pres].index.min()- timedelta(days=60), end=dfEo[dfEo.President == pres].index.max(), freq='M')
x = dfEo[dfEo.President==pres].groupby(pd.TimeGrouper('M')).Order.count()
fig, ax = plt.subplots()
ax.set(title=pres, ylabel='Number of Executive Orders', xlabel='Month')
plt.xticks(rotation=70)
ax.plot(x.index, x)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))
plt.savefig(os.path.join(eo_path, pres.replace(' ', '-')+'.png'))
In [9]:
eo_path = './Executive Orders/'
pres='Barack Obama'
if not os.path.isfile(os.path.join(eo_path, pres.replace(' ', '-')+'.png')):
print(pres)
dateRange = pd.DatetimeIndex(start=dfEo[dfEo.President == pres].index.min()- timedelta(days=60), end=dfEo[dfEo.President == pres].index.max(), freq='M')
x = dfEo[dfEo.President==pres].groupby(pd.TimeGrouper('M')).Order.count()
fig, ax = plt.subplots()
ax.set(title=pres, ylabel='Number of Executive Orders', xlabel='Month')
plt.xticks(rotation=70)
ax.plot(x.index, x)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))
plt.savefig(os.path.join(eo_path, pres.replace(' ', '-')+'.png'))
In [ ]:
In [10]:
ct = pd.crosstab(dfEo.President, dfEo.Order.count())
ct.hist(column=)
#plt.bar(ct)
In [ ]:
In [ ]:
In [ ]: