Skip to content

Instantly share code, notes, and snippets.

@thearchduke
Last active August 3, 2017 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thearchduke/70adb24e14134e4a62cd9bb8fd4d895f to your computer and use it in GitHub Desktop.
Save thearchduke/70adb24e14134e4a62cd9bb8fd4d895f to your computer and use it in GitHub Desktop.
Donald Trump Gallup approval rating weighted by party identification
from to weighted_rep weighted_ind weighted_dem unweighted_rep unweighted_ind unweighted_dem
1/20/17 1/29/17 0.2492 0.1848 0.0325 0.89 0.42 0.13
1/30/17 2/5/17 0.2666 0.1517 0.0248 0.86 0.41 0.08
2/6/17 2/12/17 0.2697 0.1295 0.0341 0.87 0.35 0.11
2/13/17 2/19/17 0.2666 0.1369 0.0217 0.86 0.37 0.07
2/20/17 2/26/17 0.2728 0.1406 0.031 0.88 0.38 0.1
2/27/17 3/5/17 0.2288 0.1638 0.03 0.88 0.39 0.1
3/6/17 3/12/17 0.2288 0.1512 0.027 0.88 0.36 0.09
3/13/17 3/19/17 0.2236 0.147 0.03 0.86 0.35 0.1
3/20/17 3/26/17 0.2184 0.1386 0.024 0.84 0.33 0.08
3/27/17 4/2/17 0.2106 0.1512 0.018 0.81 0.36 0.06
4/3/17 4/9/17 0.2175 0.1496 0.0168 0.87 0.34 0.06
4/10/17 4/16/17 0.2175 0.1584 0.028 0.87 0.36 0.1
4/17/17 4/23/17 0.215 0.1584 0.0252 0.86 0.36 0.09
4/24/17 4/30/17 0.2175 0.1584 0.0336 0.87 0.36 0.12
5/1/17 5/7/17 0.2436 0.16 0.0252 0.84 0.4 0.09
5/8/17 5/14/17 0.2436 0.14 0.0224 0.84 0.35 0.08
5/15/17 5/21/17 0.2436 0.124 0.0196 0.84 0.31 0.07
5/22/17 5/28/17 0.2523 0.148 0.0224 0.87 0.37 0.08
5/29/17 6/4/17 0.2378 0.136 0.0196 0.82 0.34 0.07
6/5/17 6/11/17 0.2158 0.1302 0.024 0.83 0.31 0.08
6/12/17 6/18/17 0.2184 0.1344 0.018 0.84 0.32 0.06
6/19/17 6/25/17 0.221 0.1428 0.018 0.85 0.34 0.06
6/26/17 7/2/17 0.221 0.1512 0.024 0.85 0.36 0.08
7/3/17 7/9/17 0.2125 0.1575 0.0224 0.85 0.35 0.08
7/10/17 7/16/17 0.2175 0.1485 0.0224 0.87 0.33 0.08
7/17/17 7/23/17 0.215 0.1395 0.0224 0.86 0.31 0.08
7/24/17 7/30/17 0.205 0.144 0.0196 0.82 0.32 0.07
'''
Script to parse Trump approval (sourced from Gallup website,
table copy+paste from browser), weighted by party ID, and output as CSV
'''
import csv
import datetime
import string
#Republicans Independents Democrats
PARTY_ID = '''
2017 Jul 5-9 25 45 28
2017 Jun 7-11 26 42 30
2017 May 3-7 29 40 28
2017 Apr 5-9 25 44 28
2017 Mar 1-5 26 42 30
2017 Feb 1-5 31 37 31
2017 Jan 4-8 28 44 25
'''
#Republicans + Republican leaners Democrats + Democratic leaners
INDIE_LEANERS = '''
2017 Jul 5-9 40 48
2017 Jun 7-11 43 49
2017 May 3-7 45 44
2017 Apr 5-9 41 48
2017 Mar 9-29 38 47
2017 Mar 1-5 41 49
2017 Feb 1-5 43 48
2017 Jan 4-8 44 43
'''
#Republicans Independents Democrats
TRUMP_APPROVAL = '''
2017 Jul 24-30 82 32 7
2017 Jul 17-23 86 31 8
2017 Jul 10-16 87 33 8
2017 Jul 3-9 85 35 8
2017 Jun 26-Jul 2 85 36 8
2017 Jun 19-25 85 34 6
2017 Jun 12-18 84 32 6
2017 Jun 5-11 83 31 8
2017 May 29-Jun 4 82 34 7
2017 May 22-28 87 37 8
2017 May 15-21 84 31 7
2017 May 8-14 84 35 8
2017 May 1-7 84 40 9
2017 Apr 24-30 87 36 12
2017 Apr 17-23 86 36 9
2017 Apr 10-16 87 36 10
2017 Apr 3-9 87 34 6
2017 Mar 27-Apr 2 81 36 6
2017 Mar 20-26 84 33 8
2017 Mar 13-19 86 35 10
2017 Mar 6-12 88 36 9
2017 Feb 27-Mar 5 88 39 10
2017 Feb 20-26 88 38 10
2017 Feb 13-19 86 37 7
2017 Feb 6-12 87 35 11
2017 Jan 30-Feb 5 86 41 8
2017 Jan 20-29 89 42 13
'''
from_date_fmt = '%Y %b %d'
to_date_fmt_no_month = '%d'
to_date_fmt_with_month = '%b %d'
def get_range_from_line(line):
date_range_str = ' '.join(line.split()[0:3])
from_date_str = date_range_str.split('-')[0]
from_date = datetime.datetime.strptime(from_date_str, from_date_fmt).date()
after_dash = line.split('-')[1]
if after_dash[0] in string.letters:
to_date_str = ' '.join(after_dash.split()[0:2])
to_date_month_day = datetime.datetime.strptime(
to_date_str, to_date_fmt_with_month
)
to_date = datetime.date(
from_date.year, to_date_month_day.month, to_date_month_day.day
)
else:
to_date_str = after_dash.split()[0]
to_date_day = datetime.datetime.strptime(to_date_str, to_date_fmt_no_month)
to_date = datetime.date(
from_date.year, from_date.month, to_date_day.day
)
return (from_date, to_date)
def parse_id_approval(which):
output = []
for line in which.split('\n'):
if line == '':
continue
date_range = get_range_from_line(line)
rep, ind, dem = line.split(' ')[-3:]
output.append({
'from': date_range[0],
'to': date_range[1],
'rep': int(rep)/100.0,
'ind': int(ind)/100.0,
'dem': int(dem)/100.0,
})
return tuple(output)
def make_weighted_approvals(id_ranges, approval_ranges):
output = []
for poll in approval_ranges:
which_id_range = None
for i, id_range in enumerate(id_ranges):
if id_range['from'] < poll['to']:
index = i-1 if i > 0 else i
which_id_range = id_ranges[i]
break
output.append({
'from': poll['from'],
'to': poll['to'],
'weighted_rep': poll['rep'] * which_id_range['rep'],
'weighted_ind': poll['ind'] * which_id_range['ind'],
'weighted_dem': poll['dem'] * which_id_range['dem'],
'unweighted_rep': poll['rep'],
'unweighted_ind': poll['ind'],
'unweighted_dem': poll['dem'],
})
return output
def write_csv():
id_ranges = parse_id_approval(PARTY_ID)
approval_ranges = parse_id_approval(TRUMP_APPROVAL)
weighted_approvals = make_weighted_approvals(id_ranges, approval_ranges)
with open('weighted_trump_approval.csv', 'w') as fout:
writer = csv.writer(fout)
keys = ('from', 'to', 'weighted_rep', 'weighted_ind', 'weighted_dem',
'unweighted_rep', 'unweighted_ind', 'unweighted_dem'
)
writer.writerow(keys)
for approval in weighted_approvals:
writer.writerow([approval[k] for k in keys])
if __name__ == '__main__':
write_csv()
@thearchduke
Copy link
Author

thearchduke commented Aug 3, 2017

Example rendering
Example 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment