# import packages 
import pandas as pd                   # data management
import matplotlib.pyplot as plt       # graphics 
import datetime as dt                 # check today's date 
import sys                            # check Python version 

# IPython command, puts plots in notebook 
%matplotlib inline

print('Today is', dt.date.today())
print('Python version:\n', sys.version, sep='') 

"""
Explanation: Data Bootcamp "Learning Experience"
NYU Stern School of Business | March 2016
Please answer the questions below in this IPython notebook.  Add cells as needed.  When you're done, save it and email to Dave Backus (db3@nyu.edu).  Use the subject line:  "bootcamp exam" plus "UG" or "MBA", as appropriate.  Make sure you have the correct email address.  And the correct file.  Doing this correctly is worth 10 points.  
This IPython notebook was created by Dave Backus, Chase Coleman, and Spencer Lyon for the NYU Stern course Data Bootcamp.  
Import packages
Run this code.  Really.
End of explanation
"""


# experiment in this box 

a = 2*3
b = 2.0*3
c = 'abc'
d = ['This', "is", 'not', "a", 'string']
e = d[3]

# do this with a function because we're lazy and value our time
def valuetype(x):
    """
    print value and type of input x
    """
    print('Value and type: ', x, ', ', type(x), sep='')

# (a)
valuetype(a)

# (b)
valuetype(b)

# (c)
valuetype(c)

# (d)
valuetype(d)

# (e)
valuetype(e)

"""
Explanation: Question 0

Change the file name by adding _YourLastName to it in the textbox at the top.  
Add a markdown cell directly above this one that includes your name in bold, your student number, and your email address.  

(10 points)
Question 1
For each part (a)-(e), describe the type and value of the variable with the corresponding name:
(a) a = 2*3 
(b) b = 2.0*3
(c) c = 'abc'
(d) d = ['This', "is", 'not', "a", 'string']
(e) e = d[3]
(25 points)
End of explanation
"""


f = (1, 2, 3)
g = {1: 'Chase', 2: 'Dave', 3: 'Spencer'}
h = 'foo' + 'bar'
i = (1 != 0) 

# (f)
valuetype(f)

# (g)
valuetype(g)

# (h)
valuetype(h)

# (i)
valuetype(i)

"""
Explanation: Question 2
As above describe the value and type of each variable.  (These are more challenging.)   
(f) f = (1, 2, 3)
(g) g = {1: 'Chase', 2: 'Dave', 3: 'Spencer'} 
(h) h = 'foo' + 'bar'
(i) i = (1 != 0)   # parens not needed, but they make code more understandable
(20 points)
End of explanation
"""


torf = True

if torf: 
    x = 1
else:
    x = 2
    
print('x =', x) 

"""
Explanation: Question 3
Explain the code below -- briefly -- in a Markdown cell.  What happens if we change the first line to torf = False? 
(10 points)
End of explanation
"""


first = 'Sarah'
last  = 'Beckett-Hile' 

# (a)
firstoffirst = first[0]
firstoffirst

# (b) 
last.split('-')

# (c) 
combo = first + ' ' + last[0] + '.'
combo

# (d) 
def lastinitial(name1, name2):
    combo = name1 + ' ' + name2[0] + '.'
    return combo

lastinitial(first, last)

lastinitial('Chase', 'Coleman')

"""
Explanation: Changed cell to Markdown with menu at top 
The code from if on:

if torf is True, set x=1
if torf is False, we set x=2

At the top, torf is True, so we do the first one (x=1).  If we change it to False, we do the second one (x=2).
Question 4
Take the first and last variables defined in the cell below and do the following with them:  
(a) Extract the first letter of last.  
(b) Find a method to split last into two components at the hyphen.  
(c) Define a new string variable named combo consisting of first (the first name), a space, the first letter of last, and a period.  
(d) Define a function that takes as inputs first and last names (both strings) and returns combo (also a string, consisting of the first name plus the first letter of the last name and a period).  Apply it to the variables first and last and to your own first and last names.  
(20 points)
End of explanation
"""


things = [1, '2', 3.0, 'four']

# (a) 
for thing in things:
    print('Value and type: ', thing, ', ', type(thing), sep='')

# (b) 
for thing in things:
    if type(thing) == int:
        print('Value and type: ', thing, ', ', type(thing), sep='')

# (c) 
[print('Value and type: ', thing, ', ', type(thing), sep='') for thing in things]

[print('Value and type: ', thing, ', ', type(thing), sep='') for thing in things
    if type(thing) == int]

"""
Explanation: Question 5
Consider the variable things = [1, '2', 3.0, 'four'].  
(a) Write a loop that goes through the elements of things and prints them and their type.
(b) Modify the loop to print only those elements that are integers. 
(10 points) 
(c) Bonus (not graded): Can you do parts (a) and (b) with a list comprehension?
End of explanation
"""


data = {'Size': ['1 to 4', '5 to 9', '10 to 19', '20 to 49', '50 to 99',
                 '100 to 249', '250 to 499', '500 to 999', '1000 to 2499',
                 '2500 to 4999', '5000 to 9999', '10000+'], 
        'Firms': [2846416, 1020772, 598153, 373345, 115544, 63845,
                  19389, 9588, 6088, 2287, 1250, 1357], 
        'Emp': [5998912, 6714924, 8151891, 11425545, 8055535, 9788341, 
                6611734, 6340775, 8321486, 6738218, 6559020, 32556671]}
bds = pd.DataFrame(data) 
bds = bds.set_index('Size')

# (a)
type(bds)

# (b)
bds.shape

(c)
list(bds)  # or bsd.columns

bds.index

# (d)
bds.dtypes

"""
Explanation: Question 6
Next up:  We explore the Census's Business Dynamics Statistics, a huge collection of data about firms.  We've extracted a small piece of one of their databases that includes these variables for 2013:

Size:  size category of firms based on number of employees 
Firms:  number of firms in this size category
Emp:  number of employees in this size category 

Run the code cell below to load the data and use the result to answer these questions:  
(a) What type of object is bsd?
(b) What are its dimensions?
(c) What are its column labels?  Row labels?
(d) What dtypes are the columns?   
(20 points)
End of explanation
"""


# (a) 
bds['AvgEmp'] = bds['Emp']/bds['Firms']
bds.head(3)

# (b) 
bds = bds.rename(columns={'Emp': 'Employment'})
bds.head(3)

# (c)
bds['Employment'].plot.bar()

"""
Explanation: Question 7
Continuing with the same data:  
(a) Create a new variable AvgEmp equal to the ratio of Emp to Firms and add it as a new column in bsd.
(b) Use a dataframe method to change the name of Emp to Employees. 
(c) Create a bar chart of the number of employees in each size category.  
(15 points)
End of explanation
"""


# everything has to be in same cell to apply to the same figure
plt.style.use('fivethirtyeight')  # (e) 
fig, ax = plt.subplots()  # (a) 
bds['Firms'].plot.barh(ax=ax, color='red')  # (b,c) 
ax.set_title('Numbers of firms by employment category')  # (d) 

"""
Explanation: Question 8
Still continuing with the same data:  
(a) Create figure and axis objects.  
(b) Add a horizontal bar chart of the number of firms in each category to the axis object you created. 
(c) Make the bars red. 
(d) Add a title.
(e) Change the style to fivethirtyeight.  
(25 points)
End of explanation
"""


