{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Open up a NEW NOTEBOOK\n", "\n", "Have it be in the same directory as `CD_Transactions_07-23-2017.CSV`\n", "\n", "# Open up a SHELL\n", "\n", "`cd` to the same directory as `CD_Transactions_07-23-2017.CSV`." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "03-classwork.zip indiv_header_file.csv\r\n", "CD_Transactions_07-23-2017.CSV links.txt\r\n", "Sqlite and pandas.ipynb\r\n" ] } ], "source": [ "ls" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 3.17 s, sys: 278 ms, total: 3.45 s\n", "Wall time: 3.51 s\n" ] } ], "source": [ "%%time\n", "df = pd.read_csv(\"CD_Transactions_07-23-2017.CSV\")\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# .import CD_Transactions_07-23-2017.CSV contribs" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "03-classwork.zip alaska.db\r\n", "CD_Transactions_07-23-2017.CSV indiv_header_file.csv\r\n", "Sqlite and pandas.ipynb links.txt\r\n" ] } ], "source": [ "ls" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "import sqlite3" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Connect to our database\n", "conn = sqlite3.connect(\"alaska.db\")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 6.42 s, sys: 1.08 s, total: 7.5 s\n", "Wall time: 8.68 s\n" ] } ], "source": [ "%%time\n", "# Make our query, get the results from the database\n", "# give it the QUERY and give it the CONNECTION\n", "df = pd.read_sql(\"SELECT * FROM contribs\", conn)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ResultDateTransaction TypePayment TypePayment DetailAmountLast/Business NameFirst NameAddressCity...--------Report TypeElection NameElection TypeMunicipalityOfficeFiler TypeNameReport YearSubmitted
014/3/2017IncomeCheck12864$350.00Alaska Republican Party State Account...24 Hour Report2017 - Anchorage Municipal ElectionAnchorage MunicipalAnchorage, Municipality ofSchool BoardCandidateKay E Schuster20174/3/2017
124/3/2017IncomeCredit Card$500.00CoffeyDan...24 Hour Report2017 - Anchorage Municipal ElectionAnchorage MunicipalAnchorage, Municipality ofSchool BoardCandidateKay E Schuster20174/3/2017
234/3/2017IncomeCheck3047$300.00ACS Employees PAC...24 Hour Report2017 - Anchorage Municipal ElectionAnchorage MunicipalAnchorage, Municipality ofAssemblyCandidatePete Petersen20174/3/2017
344/3/2017IncomeCredit Card$500.00HolmesPatrick...24 Hour Report2017 - Anchorage Municipal ElectionAnchorage MunicipalAnchorage, Municipality ofGroupAlaska Republican Party20174/3/2017
454/3/2017IncomeCredit Card$500.00GonzalesMark L....24 Hour Report2017 - Anchorage Municipal ElectionAnchorage MunicipalAnchorage, Municipality ofGroupAlaska Republican Party20174/3/2017
\n", "

5 rows × 26 columns

\n", "
" ], "text/plain": [ " Result Date Transaction Type Payment Type Payment Detail Amount \\\n", "0 1 4/3/2017 Income Check 12864 $350.00 \n", "1 2 4/3/2017 Income Credit Card $500.00 \n", "2 3 4/3/2017 Income Check 3047 $300.00 \n", "3 4 4/3/2017 Income Credit Card $500.00 \n", "4 5 4/3/2017 Income Credit Card $500.00 \n", "\n", " Last/Business Name First Name Address City ... \\\n", "0 Alaska Republican Party State Account ... \n", "1 Coffey Dan ... \n", "2 ACS Employees PAC ... \n", "3 Holmes Patrick ... \n", "4 Gonzales Mark L. ... \n", "\n", " -------- Report Type Election Name \\\n", "0 24 Hour Report 2017 - Anchorage Municipal Election \n", "1 24 Hour Report 2017 - Anchorage Municipal Election \n", "2 24 Hour Report 2017 - Anchorage Municipal Election \n", "3 24 Hour Report 2017 - Anchorage Municipal Election \n", "4 24 Hour Report 2017 - Anchorage Municipal Election \n", "\n", " Election Type Municipality Office Filer Type \\\n", "0 Anchorage Municipal Anchorage, Municipality of School Board Candidate \n", "1 Anchorage Municipal Anchorage, Municipality of School Board Candidate \n", "2 Anchorage Municipal Anchorage, Municipality of Assembly Candidate \n", "3 Anchorage Municipal Anchorage, Municipality of Group \n", "4 Anchorage Municipal Anchorage, Municipality of Group \n", "\n", " Name Report Year Submitted \n", "0 Kay E Schuster 2017 4/3/2017 \n", "1 Kay E Schuster 2017 4/3/2017 \n", "2 Pete Petersen 2017 4/3/2017 \n", "3 Alaska Republican Party 2017 4/3/2017 \n", "4 Alaska Republican Party 2017 4/3/2017 \n", "\n", "[5 rows x 26 columns]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 45.2 ms, sys: 7.88 ms, total: 53.1 ms\n", "Wall time: 69.8 ms\n" ] } ], "source": [ "%%time\n", "# df[df.Office == 'School Board'].head()\n", "df = pd.read_sql(\"SELECT * FROM contribs WHERE Office = 'School Board'\", conn)\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ResultDateTransaction TypePayment TypePayment DetailAmountLast/Business NameFirst NameAddressCity...--------Report TypeElection NameElection TypeMunicipalityOfficeFiler TypeNameReport YearSubmitted
014/3/2017IncomeCheck12864$350.00Alaska Republican Party State Account...24 Hour Report2017 - Anchorage Municipal ElectionAnchorage MunicipalAnchorage, Municipality ofSchool BoardCandidateKay E Schuster20174/3/2017
124/3/2017IncomeCredit Card$500.00CoffeyDan...24 Hour Report2017 - Anchorage Municipal ElectionAnchorage MunicipalAnchorage, Municipality ofSchool BoardCandidateKay E Schuster20174/3/2017
234/3/2017IncomeCheck3047$300.00ACS Employees PAC...24 Hour Report2017 - Anchorage Municipal ElectionAnchorage MunicipalAnchorage, Municipality ofAssemblyCandidatePete Petersen20174/3/2017
344/3/2017IncomeCredit Card$500.00HolmesPatrick...24 Hour Report2017 - Anchorage Municipal ElectionAnchorage MunicipalAnchorage, Municipality ofGroupAlaska Republican Party20174/3/2017
454/3/2017IncomeCredit Card$500.00GonzalesMark L....24 Hour Report2017 - Anchorage Municipal ElectionAnchorage MunicipalAnchorage, Municipality ofGroupAlaska Republican Party20174/3/2017
\n", "

5 rows × 26 columns

\n", "
" ], "text/plain": [ " Result Date Transaction Type Payment Type Payment Detail Amount \\\n", "0 1 4/3/2017 Income Check 12864 $350.00 \n", "1 2 4/3/2017 Income Credit Card $500.00 \n", "2 3 4/3/2017 Income Check 3047 $300.00 \n", "3 4 4/3/2017 Income Credit Card $500.00 \n", "4 5 4/3/2017 Income Credit Card $500.00 \n", "\n", " Last/Business Name First Name Address City ... \\\n", "0 Alaska Republican Party State Account ... \n", "1 Coffey Dan ... \n", "2 ACS Employees PAC ... \n", "3 Holmes Patrick ... \n", "4 Gonzales Mark L. ... \n", "\n", " -------- Report Type Election Name \\\n", "0 24 Hour Report 2017 - Anchorage Municipal Election \n", "1 24 Hour Report 2017 - Anchorage Municipal Election \n", "2 24 Hour Report 2017 - Anchorage Municipal Election \n", "3 24 Hour Report 2017 - Anchorage Municipal Election \n", "4 24 Hour Report 2017 - Anchorage Municipal Election \n", "\n", " Election Type Municipality Office Filer Type \\\n", "0 Anchorage Municipal Anchorage, Municipality of School Board Candidate \n", "1 Anchorage Municipal Anchorage, Municipality of School Board Candidate \n", "2 Anchorage Municipal Anchorage, Municipality of Assembly Candidate \n", "3 Anchorage Municipal Anchorage, Municipality of Group \n", "4 Anchorage Municipal Anchorage, Municipality of Group \n", "\n", " Name Report Year Submitted \n", "0 Kay E Schuster 2017 4/3/2017 \n", "1 Kay E Schuster 2017 4/3/2017 \n", "2 Pete Petersen 2017 4/3/2017 \n", "3 Alaska Republican Party 2017 4/3/2017 \n", "4 Alaska Republican Party 2017 4/3/2017 \n", "\n", "[5 rows x 26 columns]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_sql(\"SELECT * FROM contribs\", conn)\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "df.to_csv(\"just-alaska.csv\", index=False)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ResultDateTransaction TypePayment TypePayment DetailAmountLast/Business NameFirst NameAddressCityStateZipCountryOccupationEmployerPurpose of Expenditure--------Report TypeElection NameElection TypeMunicipalityOfficeFiler TypeNameReport YearSubmitted
014/3/2017IncomeCheck12864$350.00Alaska Republican Party State AccountAlaskaUSAN/AN/A24 Hour Report2017 - Anchorage Municipal ElectionAnchorage MunicipalAnchorage, Municipality ofSchool BoardCandidateKay E Schuster20174/3/2017
124/3/2017IncomeCredit Card$500.00CoffeyDanAlaskaUSAAttorneySelf-Employed24 Hour Report2017 - Anchorage Municipal ElectionAnchorage MunicipalAnchorage, Municipality ofSchool BoardCandidateKay E Schuster20174/3/2017
234/3/2017IncomeCheck3047$300.00ACS Employees PACAlaskaUSAPACPAC24 Hour Report2017 - Anchorage Municipal ElectionAnchorage MunicipalAnchorage, Municipality ofAssemblyCandidatePete Petersen20174/3/2017
344/3/2017IncomeCredit Card$500.00HolmesPatrickAlaskaUSAAssociate ManagerJuneau 1 LLCIndividual Contribution24 Hour Report2017 - Anchorage Municipal ElectionAnchorage MunicipalAnchorage, Municipality ofGroupAlaska Republican Party20174/3/2017
454/3/2017IncomeCredit Card$500.00GonzalesMark L.AlaskaUSAExecutiveAlcohol Detection Services of AlaskaIndividual Contribution24 Hour Report2017 - Anchorage Municipal ElectionAnchorage MunicipalAnchorage, Municipality ofGroupAlaska Republican Party20174/3/2017
\n", "
" ], "text/plain": [ " Result Date Transaction Type Payment Type Payment Detail Amount \\\n", "0 1 4/3/2017 Income Check 12864 $350.00 \n", "1 2 4/3/2017 Income Credit Card $500.00 \n", "2 3 4/3/2017 Income Check 3047 $300.00 \n", "3 4 4/3/2017 Income Credit Card $500.00 \n", "4 5 4/3/2017 Income Credit Card $500.00 \n", "\n", " Last/Business Name First Name Address City State Zip \\\n", "0 Alaska Republican Party State Account Alaska \n", "1 Coffey Dan Alaska \n", "2 ACS Employees PAC Alaska \n", "3 Holmes Patrick Alaska \n", "4 Gonzales Mark L. Alaska \n", "\n", " Country Occupation Employer \\\n", "0 USA N/A N/A \n", "1 USA Attorney Self-Employed \n", "2 USA PAC PAC \n", "3 USA Associate Manager Juneau 1 LLC \n", "4 USA Executive Alcohol Detection Services of Alaska \n", "\n", " Purpose of Expenditure -------- Report Type \\\n", "0 24 Hour Report \n", "1 24 Hour Report \n", "2 24 Hour Report \n", "3 Individual Contribution 24 Hour Report \n", "4 Individual Contribution 24 Hour Report \n", "\n", " Election Name Election Type \\\n", "0 2017 - Anchorage Municipal Election Anchorage Municipal \n", "1 2017 - Anchorage Municipal Election Anchorage Municipal \n", "2 2017 - Anchorage Municipal Election Anchorage Municipal \n", "3 2017 - Anchorage Municipal Election Anchorage Municipal \n", "4 2017 - Anchorage Municipal Election Anchorage Municipal \n", "\n", " Municipality Office Filer Type \\\n", "0 Anchorage, Municipality of School Board Candidate \n", "1 Anchorage, Municipality of School Board Candidate \n", "2 Anchorage, Municipality of Assembly Candidate \n", "3 Anchorage, Municipality of Group \n", "4 Anchorage, Municipality of Group \n", "\n", " Name Report Year Submitted \n", "0 Kay E Schuster 2017 4/3/2017 \n", "1 Kay E Schuster 2017 4/3/2017 \n", "2 Pete Petersen 2017 4/3/2017 \n", "3 Alaska Republican Party 2017 4/3/2017 \n", "4 Alaska Republican Party 2017 4/3/2017 " ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.set_option(\"display.max_columns\", 100)\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " 24751\n", "J 7229\n", "Michael 4881\n", "D 4514\n", "John 4296\n", "R 4170\n", "Robert 4127\n", "C 3892\n", "M 3876\n", "James 3722\n", "David 3443\n", "S 3222\n", "A 2804\n", "B 2793\n", "T 2615\n", "William 2222\n", "K 2182\n", "Richard 2114\n", "MICHAEL 2082\n", "JAMES 1986\n", "Brian 1845\n", "Daniel 1839\n", "JOHN 1779\n", "L 1728\n", "G 1672\n", "Mark 1665\n", "Eric 1641\n", "ROBERT 1607\n", "Scott 1588\n", "Thomas 1568\n", " ... \n", "Cassamdra 1\n", "AERIALE 1\n", "Mildred P. 1\n", "MYLA 1\n", "Woolsey 1\n", "CAROL ANN 1\n", "SHAWNAH 1\n", "M R 1\n", "Kelin 1\n", "LEA B 1\n", "META 1\n", "Oran 1\n", "ETHAN 1\n", "Caherine 1\n", "Zuzanna 1\n", "Darrelle 1\n", "Debe 1\n", "ROBERTO 1\n", "Ilaura 1\n", "MARNI 1\n", " Jake 1\n", "Hugh (Bud) 1\n", " Ervon 1\n", "Saraya 1\n", " Boko 1\n", "JOYLLE 1\n", "EUNSUK 1\n", "Chappel 1\n", "CATHARINE 1\n", "Gabriel 1\n", "Name: First Name, Length: 15813, dtype: int64" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df['First Name'].value_counts()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }