Setup Notebook


In [2]:
# Standard library
import os
import sys
sys.path.append("../src/")

# Third party imports
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

In [3]:
# Customizations
sns.set() # matplotlib defaults

# Any tweaks that normally go in .matplotlibrc, etc., should explicitly go here
plt.rcParams['figure.figsize'] = (12, 8)
%config InlineBackend.figure_format='retina'

In [4]:
# Find the notebook the saved figures came from
fig_prefix = "../figures/2017-01-14-tc-"

Load Data


In [7]:
datfile = '../../zzData_RAW/P00000001-ALL.csv'

cols = ['cand_nm', 'contbr_st', 'contbr_employer', \
        'contb_receipt_amt', 'contbr_occupation', 'contb_receipt_amt', 'contb_receipt_dt']

donate = pd.read_csv(datfile, index_col=False, dtype='object', usecols = cols)
donate['contb_receipt_amt'] = pd.to_numeric(donate['contb_receipt_amt'])
# donate['contb_receipt_dt'] = pd.to_datetime(donate['contb_receipt_dt'])

donate.dtypes


Out[7]:
cand_nm               object
contbr_st             object
contbr_employer       object
contbr_occupation     object
contb_receipt_amt    float64
contb_receipt_dt      object
dtype: object

Review Data


In [8]:
import qgrid # Best practices is to put imports at the top of the Notebook.
qgrid.nbinstall(overwrite=True)

In [9]:
donate.head()


Out[9]:
cand_nm contbr_st contbr_employer contbr_occupation contb_receipt_amt contb_receipt_dt
0 Rubio, Marco 20 STRATEGIC COALITIONS & INITIATIVES LL OUTREACH DIRECTOR 175.0 15-MAR-16
1 Rubio, Marco 30 MORTGAGE CAPITAL ADVISORS PRIVATE MORTGAGE BANKING 25.0 16-MAR-16
2 Rubio, Marco AE DIPLOMAT US GOVERNMENT 100.0 20-FEB-16
3 Rubio, Marco AE DODEA/DS TEACHER 500.0 26-JUN-15
4 Rubio, Marco AE US ARMY PHYSICIAN 200.0 10-MAR-16

In [10]:
qgrid.show_grid(donate.head(), remote_js=True)

In [11]:
donate.groupby('cand_nm').mean()


Out[11]:
contb_receipt_amt
cand_nm
Bush, Jeb 1070.511192
Carson, Benjamin S. 106.105080
Christie, Christopher J. 1310.567022
Clinton, Hillary Rodham 147.170917
Cruz, Rafael Edward 'Ted' 101.012978
Fiorina, Carly 229.541594
Gilmore, James S III 1164.883068
Graham, Lindsey O. 834.992954
Huckabee, Mike 364.101526
Jindal, Bobby 1653.062954
Johnson, Gary 264.938421
Kasich, John R. 557.681819
Lessig, Lawrence 464.148245
McMullin, Evan 213.075646
O'Malley, Martin Joseph 754.424574
Pataki, George E. 1467.004215
Paul, Rand 186.913096
Perry, James R. (Rick) 1233.879504
Rubio, Marco 298.721951
Sanders, Bernard 44.711544
Santorum, Richard J. 617.390082
Stein, Jill 127.287894
Trump, Donald J. 169.609067
Walker, Scott 676.479251
Webb, James Henry Jr. 549.058013

In [56]:
sns.stripplot(x="cand_nm", y="contb_receipt_amt", data=donate[10000:20000]);



In [ ]:
#x = np.random.normal(size=100)
sns.distplot(donate['contb_receipt_amt'], bins=20, kde=False, rug=True);


---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-68-35440945695e> in <module>()
      1 #x = np.random.normal(size=100)
----> 2 sns.distplot(donate['contb_receipt_amt'], bins=20, kde=False, rug=True);

/opt/conda/lib/python3.5/site-packages/seaborn/distributions.py in distplot(a, bins, hist, kde, rug, fit, hist_kws, kde_kws, rug_kws, fit_kws, color, vertical, norm_hist, axlabel, label, ax)
    226         rug_color = rug_kws.pop("color", color)
    227         axis = "y" if vertical else "x"
--> 228         rugplot(a, axis=axis, ax=ax, color=rug_color, **rug_kws)
    229         if rug_color != color:
    230             rug_kws["color"] = rug_color

/opt/conda/lib/python3.5/site-packages/seaborn/distributions.py in rugplot(a, height, axis, ax, **kwargs)
    636     kwargs.setdefault("linewidth", 1)
    637     for pt in a:
--> 638         func(pt, 0, height, **kwargs)
    639 
    640     return ax

/opt/conda/lib/python3.5/site-packages/matplotlib/axes/_axes.py in axvline(self, x, ymin, ymax, **kwargs)
    814         trans = self.get_xaxis_transform(which='grid')
    815         l = mlines.Line2D([x, x], [ymin, ymax], transform=trans, **kwargs)
--> 816         self.add_line(l)
    817         self.autoscale_view(scalex=scalex, scaley=False)
    818         return l

/opt/conda/lib/python3.5/site-packages/matplotlib/axes/_base.py in add_line(self, line)
   1706             line.set_clip_path(self.patch)
   1707 
-> 1708         self._update_line_limits(line)
   1709         if not line.get_label():
   1710             line.set_label('_line%d' % len(self.lines))

/opt/conda/lib/python3.5/site-packages/matplotlib/axes/_base.py in _update_line_limits(self, line)
   1737             data_path = path
   1738 
-> 1739         elif any(line_trans.contains_branch_seperately(self.transData)):
   1740             # identify the transform to go from line's coordinates
   1741             # to data coordinates

/opt/conda/lib/python3.5/site-packages/matplotlib/transforms.py in contains_branch_seperately(self, transform)
   2096     def contains_branch_seperately(self, transform):
   2097         # Note, this is an exact copy of BlendedAffine2D.contains_branch_seperately
-> 2098         return self._x.contains_branch(transform), self._y.contains_branch(transform)
   2099 
   2100     @property

/opt/conda/lib/python3.5/site-packages/matplotlib/transforms.py in contains_branch(self, other)
   1217 
   1218         # check that a subtree is equal to other (starting from self)
-> 1219         for _, sub_tree in self._iter_break_from_left_to_right():
   1220             if sub_tree == other:
   1221                 return True

/opt/conda/lib/python3.5/site-packages/matplotlib/transforms.py in _iter_break_from_left_to_right(self)
   2321     def _iter_break_from_left_to_right(self):
   2322         for lh_compliment, rh_compliment in self._a._iter_break_from_left_to_right():
-> 2323             yield lh_compliment, rh_compliment + self._b
   2324         for lh_compliment, rh_compliment in self._b._iter_break_from_left_to_right():
   2325             yield self._a + lh_compliment, rh_compliment

/opt/conda/lib/python3.5/site-packages/matplotlib/transforms.py in __add__(self, other)
   1155         """
   1156         if isinstance(other, Transform):
-> 1157             return composite_transform_factory(self, other)
   1158         raise TypeError(
   1159             "Can not add Transform to object of type '%s'" % type(other))

/opt/conda/lib/python3.5/site-packages/matplotlib/transforms.py in composite_transform_factory(a, b)
   2471     elif isinstance(a, Affine2D) and isinstance(b, Affine2D):
   2472         return CompositeAffine2D(a, b)
-> 2473     return CompositeGenericTransform(a, b)
   2474 
   2475 

/opt/conda/lib/python3.5/site-packages/matplotlib/transforms.py in __init__(self, a, b, **kwargs)
   2283         self.output_dims = b.output_dims
   2284 
-> 2285         Transform.__init__(self, **kwargs)
   2286         self._a = a
   2287         self._b = b

/opt/conda/lib/python3.5/site-packages/matplotlib/transforms.py in __init__(self, shorthand_name)
     96         # parents are deleted, references from the children won't keep
     97         # them alive.
---> 98         self._parents = WeakValueDictionary()
     99 
    100         # TransformNodes start out as invalid until their values are

/opt/conda/lib/python3.5/weakref.py in __init__(*args, **kw)
    106         if len(args) > 1:
    107             raise TypeError('expected at most 1 arguments, got %d' % len(args))
--> 108         def remove(wr, selfref=ref(self)):
    109             self = selfref()
    110             if self is not None:

KeyboardInterrupt: 

In [ ]: