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')


2016
2012
2008
2004
2000
1996
1992
1988
1984
1980
1976
1972
1968
1964
1960
1956
1952
1948
1944
1940
1936
1932
1928
1924
1920
1916
1912
1908
1904
1900
1896
1892
1888
1884
1880
1876
1872
1868
1864
1860
1856
1852
1848
1844
1840
1836
1832
1828

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]:
col_0 Order
President Year Month
Abraham Lincoln 1861 4 3
5 1
6 1
7 2
8 1

In [7]:
ctObama = ct.loc[ct.index.get_level_values('President')=='Barack Obama']
ctObama.head()
#ctObama.index


Out[7]:
col_0 Order
President Year Month
Barack Obama 2009 1 9
2 7
3 2
4 1
5 1

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)


  File "<ipython-input-10-9ed5d39e62b9>", line 2
    ct.hist(column=)
                   ^
SyntaxError: invalid syntax

In [ ]:


In [ ]:


In [ ]: