In [1]:
import os
import charts_function_list

In [2]:
_,_,outputs = charts_function_list.folder_setup()

In [3]:
#the candidates with party
candidates = np.array([['Joe Biden','D'],
                      ['Michael Bennet','D'],
                      ['Cory Booker','D'],
                      ['Steve Bullock','D'],
                      ['Pete Buttigieg','D'],
                      ['Julián Castro','D'],
                      ['John Delaney','D'],
                      ['Bill de Blasio','D'],
                      ['Tulsi Gabbard','D'],
                      ['Kirsten Gillibrand','D'],
                      ['Mike Gravel','D'],
                      ['Kamala Harris','D'],
                      ['John Hickenlooper','D'],
                      ['Jay Inslee','D'],
                      ['Amy Klobuchar','D'],
                      ['Wayne Messam','D'],
                      ['Seth Moulton','D'],
                      ['Beto O\'Rourke','D'],
                      ['Tim Ryan','D'],
                      ['Bernie Sanders','D'],
                      ['Eric Swalwell','D'],
                      ['Donald Trump','R'],
                      ['Elizabeth Warren','D'],
                      ['Bill Weld','R'],
                      ['Marianne Williamson','D'],
                      ['Andrew Yang','D']])

In [4]:
#function to count a phrase within the candidate names
def candidate_string_search(array,term):
    return len(array[:,0][[term.upper() in x.upper() for x in array[:,0]]])

In [5]:
#sect up frame of counts, with additional counts for Dem and Rep candidates
alphabet = [chr(letter) for letter in range(65,91)]
letter_track = pd.DataFrame([(letter,candidate_string_search(candidates,letter)) for letter in alphabet],columns=['Letter','Overall Count'])
dems = candidates[candidates[:,1]=='D']
gop = candidates[candidates[:,1]=='R']
letter_track['D'] = [candidate_string_search(dems,letter) for letter in alphabet]
letter_track['R'] = [candidate_string_search(gop,letter) for letter in alphabet]
letter_track = letter_track.sort_index(ascending=False)

In [8]:
#the charts
fig, ax = plt.subplots(figsize=(11,8))
fig.suptitle('How Many 2020 Presidential Candidates\nHave Each Letter in Their Names?',size=26)

ax.set_yticks(letter_track.index)
ax.set_xticks([x for x in range(0,letter_track['Overall Count'].max()+1,2)])
ax.set_yticklabels(letter_track['Letter'],ha='center',position=(-.02,.5))
ax.set_xlabel('# of Candidates',fontsize=18)
plt.barh(letter_track.index,letter_track['D'],color='#6698B8',label='Democrat')
plt.barh(y=letter_track.index,width=letter_track['R'],left=letter_track['D'],color='#D46B68',label='Republican')
ax.grid(zorder=2,which='major',color='white',alpha=1,axis='x')
ax.set_xticks([x for x in range(0,letter_track['Overall Count'].max()+1)], minor=True)
ax.grid(zorder=2,which='minor',color='white',alpha=1,axis='x')
ax.tick_params(labelsize=16,labelcolor='#525252',which='both',length=0)
ax.legend(fontsize=15)
ax.text(0,-.13,'Created on 5/17/2019. The field may be twice as large tomorrow',transform=ax.transAxes)
ax.text(0,-.15,'igotcharts.com, 2019',transform=ax.transAxes)
os.chdir(outputs)
charts_function_list.chart_save('pres_letters')



In [7]:
#counts and percents
for letter in alphabet:
    print(str(candidate_string_search(candidates,letter)) +' candidate(s) have a '+letter+ ' in their name')
    print("{:,.1f}%".format((candidate_string_search(candidates,letter)/len(candidates))*100) +' candidate(s) have a '+letter+ ' in their name')
    print('---')


18 candidate(s) have a A in their name
69.2% candidate(s) have a A in their name
---
13 candidate(s) have a B in their name
50.0% candidate(s) have a B in their name
---
7 candidate(s) have a C in their name
26.9% candidate(s) have a C in their name
---
9 candidate(s) have a D in their name
34.6% candidate(s) have a D in their name
---
20 candidate(s) have a E in their name
76.9% candidate(s) have a E in their name
---
0 candidate(s) have a F in their name
0.0% candidate(s) have a F in their name
---
5 candidate(s) have a G in their name
19.2% candidate(s) have a G in their name
---
7 candidate(s) have a H in their name
26.9% candidate(s) have a H in their name
---
17 candidate(s) have a I in their name
65.4% candidate(s) have a I in their name
---
5 candidate(s) have a J in their name
19.2% candidate(s) have a J in their name
---
8 candidate(s) have a K in their name
30.8% candidate(s) have a K in their name
---
18 candidate(s) have a L in their name
69.2% candidate(s) have a L in their name
---
9 candidate(s) have a M in their name
34.6% candidate(s) have a M in their name
---
15 candidate(s) have a N in their name
57.7% candidate(s) have a N in their name
---
12 candidate(s) have a O in their name
46.2% candidate(s) have a O in their name
---
3 candidate(s) have a P in their name
11.5% candidate(s) have a P in their name
---
0 candidate(s) have a Q in their name
0.0% candidate(s) have a Q in their name
---
16 candidate(s) have a R in their name
61.5% candidate(s) have a R in their name
---
12 candidate(s) have a S in their name
46.2% candidate(s) have a S in their name
---
11 candidate(s) have a T in their name
42.3% candidate(s) have a T in their name
---
8 candidate(s) have a U in their name
30.8% candidate(s) have a U in their name
---
2 candidate(s) have a V in their name
7.7% candidate(s) have a V in their name
---
6 candidate(s) have a W in their name
23.1% candidate(s) have a W in their name
---
0 candidate(s) have a X in their name
0.0% candidate(s) have a X in their name
---
7 candidate(s) have a Y in their name
26.9% candidate(s) have a Y in their name
---
1 candidate(s) have a Z in their name
3.8% candidate(s) have a Z in their name
---

In [ ]: