In [1]:
import pandas as pd

In [7]:
import random

Create your own version of the Mayoral Excuse Machine in Python that takes in a name and location, selects an excuse at random and prints an excuse (“Sorry, Richard, I was late to City Hall to meet you, I had a very rough night and woke up sluggish”) Use the “excuses.csv” in the Github repository Extra credit if you print the link to the story as well


In [3]:
df=pd.read_csv('excuse.csv')

In [4]:
df.head(5)


Out[4]:
excuse headline hyperlink
0 the fog was unexpected and did slow us down a bit De Blasio Blames 'Rough Night' and Fog for Mis... http://www.dnainfo.com/new-york/20141112/rocka...
1 we had some meetings at Gracie Mansion De Blasio 30 Minutes Late to Rockaway St. Patr... http://www.dnainfo.com/new-york/20150307/belle...
2 I had a very rough night and woke up sluggish De Blasio Blames 'Rough Night' and Fog for Mis... http://www.dnainfo.com/new-york/20141112/rocka...
3 I just woke up in the middle of the night and ... De Blasio Blames 'Rough Night' and Fog for Mis... http://www.dnainfo.com/new-york/20141112/rocka...
4 we had some stuff we had to do De Blasio 30 Minutes Late to Rockaway St. Patr... http://www.dnainfo.com/new-york/20150307/belle...

In [16]:
df = pd.read_csv('excuse.csv')
exe = random.choice(df['excuse'])
hl= df[df['excuse'] == exe ]['hyperlink']
hl


Out[16]:
6    http://www.dnainfo.com/new-york/20141112/rocka...
Name: hyperlink, dtype: object

In [6]:
name=input("What's your name? ")
location=input("What's your location? ")


What's your name? Melody
What's your location? Manhattan

In [25]:
def excuse(item):
    df = pd.read_csv('excuse.csv')
    exe = random.choice(df['excuse'])
    hl= df[df['excuse'] == exe ]['hyperlink'].iloc[0]#how to get element from pandas series:.iloc
    output= ("Sorry " + name + ", I was late to " + location + 
             " to meet you, " + exe + "." +hl) 
    return(output)

In [26]:
excuse(1)


Out[26]:
'Sorry Melody, I was late to Manhattan to meet you, we need a better system.http://www.nytimes.com/2015/05/06/nyregion/mayor-de-blasio-is-irked-by-a-subway-delay.html?ref=nyregion&_r=0'

In [ ]: