In [1]:
FROM_DATE = '1800-01-01'
TO_DATE = '2000-12-31'
In [2]:
import glob, re
import csv

catalog = []
checkouts = {}

f = open('selected_muncie_titles_102218.csv', 'r', encoding='utf-8')

r = csv.reader(f)
for row in r:
    
    try:
        author = row[0]
        title = row[1]
        accession_nbr = row[3]
        transactions = int(row[4])
        file_name = row[10]

        catalog.append([author, title, accession_nbr, transactions, file_name])
        
        if (author + '. ' + title + '.') not in checkouts:
            checkouts[author + '. ' + title + '.'] = 0
        checkouts[author + '. ' + title + '.'] += transactions
    
    except ValueError:
        pass
    
f.close()

#print(len(catalog))

#print(catalog[:3])

#print(checkouts.keys())
In [3]:
import pymysql
import statistics 

connection = pymysql.connect(host='localhost',
                             user='root',
                             password='p5a1m99',
                             db='middletown1',
                             charset='utf8',
                             cursorclass=pymysql.cursors.DictCursor)

who_read_what = []

for i, entry in enumerate(catalog):

    with connection.cursor() as cursor:
        
        sql = "SELECT count(*) FROM flattenedData " + \
                "WHERE accessionNbr=%s " + \
                "AND TRUST_THIS_CENSUS = 1 " + \
                'AND transaction_date >= \'' + FROM_DATE + '\' and transaction_date <= \'' + TO_DATE + '\' ;'
        
        cursor.execute(sql, (entry[2],))
        results = cursor.fetchall()
        for r in results:
            catalog[i][3] = r['count(*)']

    with connection.cursor() as cursor:
        
        sql = "SELECT cleanedPatronName, RACE, GENDER, BIRTH_YEAR, " + \
                "BIRTHPLACE, FATHER_BIRTHPLACE, MOTHER_BIRTHPLACE, " + \
                "MARITAL_STATUS, YEARS_MARRIED, RESIDENTIAL_STATUS, " + \
                "NUM_BOARDERS, NUM_SERVANTS, YEAR_OF_IMMIGRATION, NATURALIZATION_STATUS, " + \
                "PROFESSION_TITLE, PROFESSION_CLASS, occupation_group, occupation_rank, " + \
                "count(*) " + \
                "FROM flattenedData " + \
                "WHERE accessionNbr=%s " + \
                "AND TRUST_THIS_CENSUS = 1 " + \
                "AND transaction_date >= '" + FROM_DATE + "' and transaction_date <= '" + TO_DATE + "' " + \
                'GROUP BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18;'
        
        cursor.execute(sql, (entry[2],))
        results = cursor.fetchall()
        for r in results:
            who_read_what.append([entry[0] + '. ' + entry[1] + '.', r])
       
#print(catalog[:3])
#print()
#print(who_read_what[:10])
#print()
#print(len(who_read_what))
In [4]:
import pymysql
import statistics 

connection = pymysql.connect(host='localhost',
                             user='root',
                             password='p5a1m99',
                             db='middletown1',
                             charset='utf8',
                             cursorclass=pymysql.cursors.DictCursor)

all_readers = []

with connection.cursor() as cursor:

    sql = "SELECT cleanedPatronName, RACE, GENDER, BIRTH_YEAR, " + \
            "BIRTHPLACE, FATHER_BIRTHPLACE, MOTHER_BIRTHPLACE, " + \
            "MARITAL_STATUS, YEARS_MARRIED, RESIDENTIAL_STATUS, " + \
            "NUM_BOARDERS, NUM_SERVANTS, YEAR_OF_IMMIGRATION, NATURALIZATION_STATUS, " + \
            "PROFESSION_TITLE, PROFESSION_CLASS, occupation_group, occupation_rank, " + \
            "count(*) " + \
            "FROM flattenedData " + \
            "WHERE TRUST_THIS_CENSUS = 1 " + \
            "AND transaction_date >= '" + FROM_DATE + "' and transaction_date <= '" + TO_DATE + "' " + \
            'GROUP BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18;'

    cursor.execute(sql)
    results = cursor.fetchall()
    for r in results:
        all_readers.append(['', r])
       
#print(catalog[:3])
#print()
#print(all_readers[:10])
#print()
#print(len(all_readers))
In [5]:
from collections import Counter

def report_key_count(readership_data, key, label):
    
    total_n = 0
    results = {}
    
    for r in readership_data:
        
        if r[1][key] not in results:
            results[r[1][key]] = 0
            
        results[r[1][key]] += r[1]['count(*)']
        
        total_n += r[1]['count(*)']
        
    print()
    print(label, key)
    print()
    
    for w in Counter(results).most_common(10):
        print('\t', w[0], '{:4.1f}'.format(float(w[1] / float(total_n) * 100.0)) + '%')
    
In [6]:
for k in all_readers[0][1].keys():
    if k not in  ['count(*)', 'cleanedPatronName']:
        
        print()
        print('------------------------------------------------------')
        
        report_key_count(who_read_what, k, 'LOST CAUSE')
    
        report_key_count(all_readers, k, 'ALL_MUNCIE')
------------------------------------------------------

LOST CAUSE RACE

	 White 99.7%
	 Black  0.2%
	 Mulatto  0.1%

ALL_MUNCIE RACE

	 White 99.7%
	 Black  0.2%
	 Mulatto  0.1%

------------------------------------------------------

LOST CAUSE GENDER

	 Male 50.2%
	 Female 49.8%

ALL_MUNCIE GENDER

	 Female 53.0%
	 Male 47.0%

------------------------------------------------------

LOST CAUSE BIRTH_YEAR

	 1883  6.3%
	 1884  5.8%
	 1881  5.7%
	 1882  5.1%
	 1886  5.0%
	 1885  4.7%
	 1887  4.4%
	 1878  4.3%
	 1880  4.1%
	 1879  3.5%

ALL_MUNCIE BIRTH_YEAR

	 1881  5.5%
	 1883  5.5%
	 1884  5.4%
	 1885  4.7%
	 1880  4.5%
	 1882  4.5%
	 1886  4.4%
	 1878  4.3%
	 1887  4.1%
	 1879  3.5%

------------------------------------------------------

LOST CAUSE BIRTHPLACE

	 Indiana                        57.6%
	 Ohio                           20.6%
	 Pennsylvania                    2.8%
	 Illinois                        2.3%
	 Kentucky                        1.9%
	 Kansas                          1.6%
	 Michigan                        1.2%
	 West Virginia                   1.1%
	 New York                        1.0%
	 Missouri                        0.8%

ALL_MUNCIE BIRTHPLACE

	 Indiana                        57.1%
	 Ohio                           20.7%
	 Pennsylvania                    2.6%
	 Illinois                        2.6%
	 Kentucky                        1.8%
	 Michigan                        1.5%
	 Kansas                          1.4%
	 New York                        1.2%
	 West Virginia                   1.0%
	 Germany  0.8%

------------------------------------------------------

LOST CAUSE FATHER_BIRTHPLACE

	 Indiana                        31.9%
	 Ohio                           26.1%
	 Pennsylvania                    7.7%
	 New York                        3.4%
	 Germany  3.2%
	 Ireland  3.0%
	 Virginia                        2.6%
	 England  2.4%
	 Kentucky                        1.9%
	 Illinois                        1.8%

ALL_MUNCIE FATHER_BIRTHPLACE

	 Indiana                        30.9%
	 Ohio                           26.1%
	 Pennsylvania                    7.8%
	 New York                        3.8%
	 Germany  3.4%
	 Virginia                        3.0%
	 Ireland  2.7%
	 England  2.3%
	 Kentucky                        2.1%
	 West Virginia                   1.6%

------------------------------------------------------

LOST CAUSE MOTHER_BIRTHPLACE

	 Indiana                        39.1%
	 Ohio                           25.0%
	 Pennsylvania                    5.2%
	 New York                        3.3%
	 Kentucky                        2.8%
	 Ireland  2.3%
	 West Virginia                   2.3%
	 Germany  2.1%
	 Virginia                        1.8%
	 Illinois                        1.8%

ALL_MUNCIE MOTHER_BIRTHPLACE

	 Indiana                        37.6%
	 Ohio                           24.9%
	 Pennsylvania                    5.3%
	 New York                        3.5%
	 Kentucky                        2.8%
	 Germany  2.4%
	 Ireland  2.2%
	 Virginia                        2.1%
	 West Virginia                   1.9%
	 Maryland                        1.7%

------------------------------------------------------

LOST CAUSE MARITAL_STATUS

	 Single 60.0%
	 Married 30.8%
	 None  7.3%
	 Widowed  1.6%
	 Divorced  0.3%

ALL_MUNCIE MARITAL_STATUS

	 Single 57.6%
	 Married 33.1%
	 None  6.9%
	 Widowed  2.2%
	 Divorced  0.3%

------------------------------------------------------

LOST CAUSE YEARS_MARRIED

	 None 71.1%
	 3  1.7%
	 21  1.7%
	 17  1.5%
	 16  1.4%
	 2  1.4%
	 11  1.3%
	 8  1.1%
	 6  1.0%
	 7  1.0%

ALL_MUNCIE YEARS_MARRIED

	 None 68.8%
	 3  1.7%
	 16  1.7%
	 11  1.6%
	 21  1.4%
	 2  1.4%
	 17  1.4%
	 7  1.3%
	 4  1.3%
	 10  1.1%

------------------------------------------------------

LOST CAUSE RESIDENTIAL_STATUS

	 R H (rented house) 39.4%
	 O F H (owned house free of mortgage) 28.2%
	 O M H (owned mortgaged house) 19.6%
	 None  5.6%
	 Boarder  2.1%
	 H (house)  1.8%
	 O F F (owned farm free of mortgage)  1.5%
	 O M F (owned mortgaged farm)  0.6%
	 R F (rented farm)  0.6%
	 F (farm)  0.4%

ALL_MUNCIE RESIDENTIAL_STATUS

	 R H (rented house) 38.9%
	 O F H (owned house free of mortgage) 28.9%
	 O M H (owned mortgaged house) 19.1%
	 None  5.9%
	 Boarder  2.3%
	 H (house)  1.8%
	 O F F (owned farm free of mortgage)  1.2%
	 R F (rented farm)  0.7%
	 O M F (owned mortgaged farm)  0.5%
	 F (farm)  0.4%

------------------------------------------------------

LOST CAUSE NUM_BOARDERS

	 None 92.9%
	 1  3.7%
	 2  2.1%
	 3  0.6%
	 10  0.4%
	 5  0.3%
	 4  0.0%
	 7  0.0%
	 6  0.0%

ALL_MUNCIE NUM_BOARDERS

	 None 93.3%
	 1  3.5%
	 2  2.0%
	 3  0.6%
	 5  0.3%
	 10  0.2%
	 4  0.1%
	 7  0.0%
	 6  0.0%
	 9  0.0%

------------------------------------------------------

LOST CAUSE NUM_SERVANTS

	 None 93.7%
	 1  6.2%
	 2  0.0%

ALL_MUNCIE NUM_SERVANTS

	 None 93.3%
	 1  6.6%
	 2  0.1%
	 3  0.0%
	 21  0.0%

------------------------------------------------------

LOST CAUSE YEAR_OF_IMMIGRATION

	 None 98.2%
	 1842  0.3%
	 1875  0.2%
	 1880  0.1%
	 1872  0.1%
	 1890  0.1%
	 1888  0.1%
	 1870  0.1%
	 1885  0.1%
	 1856  0.1%

ALL_MUNCIE YEAR_OF_IMMIGRATION

	 None 98.0%
	 1842  0.3%
	 1872  0.2%
	 1880  0.1%
	 1890  0.1%
	 1875  0.1%
	 1885  0.1%
	 1870  0.1%
	 1874  0.1%
	 1873  0.1%

------------------------------------------------------

LOST CAUSE NATURALIZATION_STATUS

	 None 98.4%
	 Na (Naturalized)  1.0%
	 Un (Unknown)  0.6%

ALL_MUNCIE NATURALIZATION_STATUS

	 None 98.2%
	 Na (Naturalized)  1.2%
	 Un (Unknown)  0.6%

------------------------------------------------------

LOST CAUSE PROFESSION_TITLE

	 None 29.5%
	 At School 22.0%
	 at school (9) 10.9%
	 Laborer  2.6%
	 Clerk  1.8%
	 Bookkeeper  1.6%
	 day laborer  1.5%
	 Stenographer  1.4%
	 Student  1.3%
	 School teacher  1.2%

ALL_MUNCIE PROFESSION_TITLE

	 None 31.4%
	 At School 20.9%
	 at school (9)  9.3%
	 Laborer  2.2%
	 Clerk  1.9%
	 Bookkeeper  1.8%
	 School teacher  1.4%
	 day laborer  1.4%
	 Stenographer  1.3%
	 Student  1.3%

------------------------------------------------------

LOST CAUSE PROFESSION_CLASS

	 None 47.6%
	 At School 35.1%
	 Laborer  4.4%
	 Teacher  2.4%
	 Clerk  2.3%
	 Sales  2.2%
	 Accountant  1.6%
	 Musician  0.6%
	 Chemist  0.6%
	 Dealer  0.5%

ALL_MUNCIE PROFESSION_CLASS

	 None 49.5%
	 At School 32.6%
	 Laborer  3.7%
	 Teacher  2.8%
	 Clerk  2.3%
	 Sales  2.1%
	 Accountant  1.8%
	 Dealer  0.9%
	 Merchant  0.7%
	 Musician  0.6%

------------------------------------------------------

LOST CAUSE occupation_group

	 White Collar 56.1%
	 Blue Collar 38.8%
	 None  5.1%

ALL_MUNCIE occupation_group

	 White Collar 57.6%
	 Blue Collar 37.1%
	 None  5.3%

------------------------------------------------------

LOST CAUSE occupation_rank

	 Low White Collar 32.6%
	 High White Collar 23.4%
	 Skilled 19.6%
	 Semi-Skilled/Service Worker 10.3%
	 Unskilled/Menial Service  9.0%
	 None  5.1%

ALL_MUNCIE occupation_rank

	 Low White Collar 33.5%
	 High White Collar 24.0%
	 Skilled 19.2%
	 Semi-Skilled/Service Worker  9.9%
	 Unskilled/Menial Service  7.9%
	 None  5.4%
In [ ]: