I am working on the file "07-sympy.ipynb".
The first task is:
The definite integral is specified by passing a tuple, with the variable to be integrated (here x) and the lower and upper limits (which can be expressions).
Note that sympy includes an "infinity" object oo (two o's), which can be used in the limits of integration:
Can you write Python code for it?

sympy.integrate(sympy.exp(-x), (x, 0, sympy.oo))


I am working on the file "07-sympy.ipynb".
The first task is:
The first argument to lambdify is a tuple containing the arguments of the function to be created. In this case that's just t, the time(s) at which we want to evaluate the expression. The second argument to lambdify is the expression that we want converted into a function. The third argument, which is optional, tells lambdify that where possible it should use numpy functions. This means that we call the function using numpy arrays, it will calculate using numpy array expressions, doing the whole calculation in a single call.
We now have a function that we can directly call:
Can you write Python code for it?

print("sympy solution at t=0: {}".format(ode_function(0.0)))
print("sympy solution at t=0.5: {}".format(ode_function(0.5)))


I am working on the file "07-sympy.ipynb".
The first task is:
The standard import command is used. The init_printing command looks at your system to find the clearest way of displaying the output; this isn't necessary, but is helpful for understanding the results.
To do anything in sympy we have to explicitly tell it if something is a variable, and what name it has. There are two commands that do this. To declare a single variable, use
Can you write Python code for it?


I am working on the file "07-sympy.ipynb".
The first task is:
Then we construct the function using lambdify:
Can you write Python code for it?

from sympy.utilities.lambdify import lambdify

ode_function = lambdify((t,), ode_expression, modules='numpy')


I am working on the file "07-sympy.ipynb".
The first task is:
There is also a function representing an unevaluated derivative:
Can you write Python code for it?

sympy.Derivative(expression2, x, 2, y, 3)


I am working on the file "07-sympy.ipynb".
The first task is:
These can be useful for display, building up a calculation in stages, simplification, or when the derivative cannot be evaluated. It can be explicitly evaluated using the doit function:
Can you write Python code for it?

sympy.Derivative(expression2, x, 2, y, 3).doit()


I am working on the file "07-sympy.ipynb".
The first task is:
This can be visually compared to the previous result. However, we would often like a more precise comparison, which requires numerically evaluating the solution to the ODE at specific points.
lambdify
At the end of a symbolic calculation using sympy we will have a result that is often long and complex, and that is needed in another part of another code. We could type the appropriate expression in by hand, but this is tedious and error prone. A better way is to make the computer do it.
The example we use here is the solution to the ODE above. We have solved it symbolically, and the result is straightforward. We can also solve it numerically using scipy. We want to compare the two.
First, let us compute the scipy numerical result:
Can you write Python code for it?

from numpy import exp
from scipy.integrate import odeint
import numpy

def dydt(y, t):
    """
    Defining the ODE dy/dt = e^{-t} - y.

    Parameters
    ----------

    y : real
        The value of y at time t (the current numerical approximation)
    t : real
        The current time t

    Returns
    -------

    dydt : real
        The RHS function defining the ODE.
    """

    return exp(-t) - y

t_scipy = numpy.linspace(0.0, 1.0)
y0 = [1.0]

y_scipy = odeint(dydt, y0, t_scipy)


I am working on the file "07-sympy.ipynb".
The first task is:
This is our expectation: multiple solutions, returned as a list. We can access and manipulate these results:
Can you write Python code for it?

roots = sympy.solve(quadratic_equation, x)
xplus, xminus = sympy.symbols(('x_{+}', 'x_{-}'))
xplus = roots[0]
xminus = roots[1]


I am working on the file "07-sympy.ipynb".
The first task is:
This is simple enough to solve, but we'll use symbolic methods to find the constant, by setting $t = 0$ and $y(t) = y(0) = 1$.
Can you write Python code for it?

general_solution = sympy.dsolve(ode, y(t))
value = general_solution.subs([(t,0), (y(0), 1)])
value


I am working on the file "07-sympy.ipynb".
The first task is:
To declare multiple variables at once, use
Can you write Python code for it?


I am working on the file "07-sympy.ipynb".
The first task is:
We can explicitly set limits, for example
Can you write Python code for it?

sympy.plot(sympy.exp(-x)*sympy.sin(x**2), (x, 0, 1));


I am working on the file "07-sympy.ipynb".
The first task is:
We can plot the solution to the differential equation computed above:
Can you write Python code for it?

sympy.plot(ode_solution.rhs, xlim=(0, 1), ylim=(0.7, 1.05));


I am working on the file "07-sympy.ipynb".
The first task is:
We can substitute in specific values for the parameters to find solutions:
Can you write Python code for it?

xplus_solution = xplus.subs([(a,1), (b,2), (c,3)])
xplus_solution


I am working on the file "07-sympy.ipynb".
The first task is:
We can use similar syntax to solve systems of equations, such as
$$ \begin{aligned}  x + 2 y &= 0, \ xy & = z_0. \end{aligned} $$
Can you write Python code for it?

eq1 = sympy.Eq(x+2*y, 0)
eq2 = sympy.Eq(x*y, z0)
sympy.solve([eq1, eq2], [x, y])


I am working on the file "07-sympy.ipynb".
The first task is:
We can use similar syntax to solve systems of equations, such as
\begin{align}
  x + 2 y &= 0, \ xy & = z_0.
\end{align}
Can you write Python code for it?


I am working on the file "07-sympy.ipynb".
The first task is:
We have a list of substitutions. Each substitution is given by a tuple, containing the variable to be replaced, and the expression replacing it. We do not have to substitute in numbers, as here, but could use other variables:
Can you write Python code for it?

xminus_solution = xminus.subs([(b,a), (c,a+z0)])
xminus_solution

xminus_solution.simplify()


I am working on the file "07-sympy.ipynb".
The first task is:
We see good visual agreement everywhere. But how accurate is it?
Now that we have numpy arrays explicitly containing the solutions, we can manipulate these to see the differences between solutions:
Can you write Python code for it?

pyplot.semilogy(t_scipy, numpy.abs(y_scipy[:,0]-y_sympy))
pyplot.xlabel(r'$t$')
pyplot.ylabel('Difference in solutions');


I am working on the file "07-sympy.ipynb".
The first task is:
We then define the differential equation. sympy.Eq defines the equation, and diff differentiates:
Can you write Python code for it?

ode = sympy.Eq(y(t).diff(t), sympy.exp(-t) - y(t))
ode


I am working on the file "07-sympy.ipynb".
The first task is:
We then find the specific solution of the ODE.
Can you write Python code for it?

ode_solution = general_solution.subs([(value.rhs,value.lhs)])
ode_solution


I am working on the file "07-sympy.ipynb".
The first task is:
We want to evaluate our sympy solution at the same points as our scipy solution, in order to do a direct comparison. In order to do that, we want to construct a function that computes our sympy power series solution, without typing it in. That is what lambdify is for: it creates a function from a sympy expression.
First let us get the expression explicitly:
Can you write Python code for it?

ode_expression = ode_solution.rhs
ode_expression


I am working on the file "07-sympy.ipynb".
The first task is:
We want to evaluate our sympy solution at the same points as our scipy solution, in order to do a direct comparison. In order to do that, we want to construct a function that computes our sympy solution, without typing it in. That is what lambdify is for: it creates a function from a sympy expression.
First let us get the expression explicitly:
Can you write Python code for it?


I am working on the file "07-sympy.ipynb".
The first task is:
What happened here? sympy is not smart enough to know that we wanted to solve for x! Instead, it solved for the first variable it encountered. Let us try again:
Can you write Python code for it?


I am working on the file "07-sympy.ipynb".
The first task is:
but there are many other options.
Solving equations
Let us go back to our quadratic equation and check the solution. To define an equation we use the sympy.Eq function:
Can you write Python code for it?

a, b, c, x = sympy.symbols(('a', 'b', 'c', 'x'))
quadratic_equation = sympy.Eq(a*x**2+b*x+c, 0)
sympy.solve(quadratic_equation)


I am working on the file "07-sympy.ipynb".
The first task is:
y is a general function, and can be a function of anything at this point (any number of variables with any name). To use it consistently, we must refer to it explicitly as a function of $t$ everywhere. For example,
Can you write Python code for it?


I am working on the file "07. 시각화/04. 웹 플롯을 위한 bokeh 패키지 소개.ipynb".
The first task is:
Bokeh 라이브러리 임포트
Bokeh는 원래 정적인 웹사이트로 렌더링 할 html 파일을 출력하는 것을 목표로 한다. 따라서 출력을 저장할 html 파일 패스를 지정해야 한다. 만약 주피터 노트북에서 작업한다면 다음과 같이 output_notebook 명령을 실행해야 한다.
Can you write Python code for it?

import bokeh.plotting as bp

# 주피터 노트북이 아닌 파일로 출력하는 경우
# bp.output_file("../images/msft_1.html", title="Bokeh Example (Static)")

# 주피터 노트북에서 실행하여 출력하는 경우
bp.output_notebook()


I am working on the file "07. 시각화/04. 웹 플롯을 위한 bokeh 패키지 소개.ipynb".
The first task is:
플롯팅
이제 플롯을 위한 준비 작업을 완료하였다. 우선 figure 명령으로 Figure 클래스 객체를 생성해야한다. 이를 p라는 변수에 저장하자.

http://bokeh.pydata.org/en/latest/docs/reference/plotting.html#bokeh.plotting.figure.figure
Can you write Python code for it?

p = bp.figure(title='Historical Stock Quotes', # 플롯 제목
              x_axis_type ='datetime',  # x 축은 날짜 정보
              tools = '')


I am working on the file "07.Loop_it_up.ipynb".
The first task is:
Adding your own input
How about adding your own input and checking against that? This doesn't come in too handy in a data science environment since you typically have a well defined dataset already. Nevertheless, this is important to know.
Can you write Python code for it?

age = int(input("Please enter your age:"))

if age < 18:
    print("You cannot vote or buy alcohol.")
elif age < 21:
    print("You can vote, but can't buy alcohol.")
else:
    print("You can vote to buy alcohol. ;) ")

mr_prez = ["Bill", "George", "Barack", "Donald"]
name = input("Enter your name:") # Don't need to specify str

type(name)

if name in mr_prez:
    print("You share your name with a President.")
else:
    print("You too can be president some day.")


I am working on the file "07.Loop_it_up.ipynb".
The first task is:
Become a Control Freak
And now, it's time to become a master of control! A data scientist needs absolute control over loops, stopping when defined conditions are met, or carrying on till a solution if found.
<img src="images/break.jpg">
Break
Can you write Python code for it?

for i in range(1,100):
    print("The square of",i,"is:",i**2)
    if i >= 5:
        break
print("Broken")


I am working on the file "07.Loop_it_up.ipynb".
The first task is:
Continue
Break's cousin is called Continue.
If a certain condition is met, carry on.
Can you write Python code for it?

letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
for letter in letters:
    print("Currently testing letter", letter)
    if letter == "e":
        print("I plead the 5th!")
        continue
    print( letter)


I am working on the file "07.Loop_it_up.ipynb".
The first task is:
Exercise

Print the names of the people in the dictionary 'data'
Print the name of the people who have 'incubees'
Print the name, and net worth of people with a net worth higher than 500,000
Print the names of people without a board seat

Enter your responses in the fields below. This is solved for you if you scroll down, but you can't cheat yourself!
Can you write Python code for it?

data = {
        "Richard": {
            "Title": "CEO",
            "Employees": ["Dinesh", "Gilfoyle", "Jared"],
            "Awards": ["Techcrunch Disrupt"],
            "Previous Firm": "Hooli",
            "Board Seat":1,
            "Net Worth": 100000
        },
        "Jared": {
            "Real_Name": "Donald",
            "Title": "CFO",
            "Previous Firm": "Hooli",
            "Board Seat":1,
            "Net Worth": 500
        },
        "Erlich": {
            "Title": "Visionary",
            "Previous Firm": "Aviato",
            "Current Firm": "Bachmannity",
            "Incubees": ["Richard", "Dinesh", "Gilfoyle", "Nelson", "Jian Yang"],
            "Board Seat": 1,
            "Net Worth": 5000000
        },
        "Nelson": {
            "Title": "Co-Founder",
            "Current Firm": "Bachmannity",
            "Previous Firm": "Hooli",
            "Board Seat": 0,
            "Net Worth": 10000000
        },
    }

# Name of people in the dictionary
data.keys()

# Alternate way to get the name of the people in the dictionary
for name in data.keys():
    print(name)

# Name of people who have incubees
for name in data.items():
    if "Incubees" in name[1]:
        print (name[0])

# Name and networth of people with a networth greater 500000
for name in data.items():
    if "Net Worth" in name[1] and name[1]["Net Worth"]>500000:
        print (name[0], name[1]["Net Worth"])

# Name of people who don't have a board seat
for name in data.items():
    if "Board Seat" in name[1] and name[1]["Board Seat"]  == 0:
        print (name[0])


I am working on the file "07.Loop_it_up.ipynb".
The first task is:
Exercise
Print all numbers from 1 to 20
Can you write Python code for it?


I am working on the file "07.Loop_it_up.ipynb".
The first task is:
Exercise
Print the square of the first 10 natural numbers.
Can you write Python code for it?


I am working on the file "07.Loop_it_up.ipynb".
The first task is:
Exercise
Write some code to check if you are old enough to buy a bottle of wine. You need to be 18 or over, but if your State is Texas, you need to be 25 or over.
Can you write Python code for it?


I am working on the file "07.Loop_it_up.ipynb".
The first task is:
How about calculating the areas of circles, given a list of radii? That too in just one line.
Can you write Python code for it?

radius = [1.0, 2.0, 3.0, 4.0, 5.0]
import math

# Area of Circle = Pi * (radius**2)

area = [round((r**2)*math.pi,2) for r in radius]
print(area)


I am working on the file "07.Loop_it_up.ipynb".
The first task is:
In a defined range, the lower number is inclusive, and upper number is exclusive. So 0 to 10 would include 0 but exclude 10. So if we need a specific range, we can use this knowledge to our advantage.
Can you write Python code for it?


I am working on the file "07.Loop_it_up.ipynb".
The first task is:
List comprehensions are very useful when dealing with an existing list. Let's see some examples.
Can you write Python code for it?

nums = [1,2,3,4,5,6,7,8,9,10]

# For every n in the list named nums, I want an n
my_list1 = [n for n in nums]
print(my_list1)

# For every n in the list named nums, I want n to be squared
my_list2 = [n**2 for n in nums]
print(my_list2)

# For every n in the list named nums, I want n, only if it is even
my_list3 = [n for n in nums if n%2 == 0]
print(my_list3)


I am working on the file "07.Loop_it_up.ipynb".
The first task is:
Loops
Time to supercharge our Python usage. Loops are in some ways, the basis for automation. Check if a condition is true, then execute a step, and keep executing it till the condition is no longer true.
Can you write Python code for it?

numbers = [1,2,3,4,5,6,7,8,9,10]

for number in numbers:
    if number % 2 == 0:
        print("Divisible by 2.")
    else:
        print("Not divisible by 2.")

numbers = {1,2,3,4,5,6,7,8,9,10}
for num in numbers:
    if num%3 == 0:
        print("Divisible by 3.")
    else:
        print("Not divisible by 3.")


I am working on the file "07.Loop_it_up.ipynb".
The first task is:
Multiple Conditions
So now we can deal with a scenario where there are two possible decisions to be made. What about more than two decision?
Say hello to "elif"!
Can you write Python code for it?

collection = [1,2,3,4,5]

if collection[0] == 0:
    print ("Zero!")
elif collection[0] == 100:
    print ("Hundred!")
else:
    print("Not Zero or Hundred")

x = ["George", "Barack", "Donald"]
test = "Richard"

if test in x:
    print(test, "has been found.")
else:
    print(test, "was not found. Let me add him to the list." )
    x.append(test)
print(x)


I am working on the file "07.Loop_it_up.ipynb".
The first task is:
Or to check for certain other conditions or properties, or to define how many times an activity will be performed.
Can you write Python code for it?


I am working on the file "07.Loop_it_up.ipynb".
The first task is:
So far, so good!
Can you write Python code for it?

# Now for List Comprehension
sqList = [num**2 for num in range(1,11)]
print(sqList)

[num**2 for num in range(1,11)]


I am working on the file "07.Loop_it_up.ipynb".
The first task is:
We can also specify a range without explicitly defining an upper or lower range, in which case, Python does it's magic: range will be 0 to one less than the number specified.
Can you write Python code for it?


I am working on the file "07.Loop_it_up.ipynb".
The first task is:
We can also use dictionary comprehension to create new dictionaries
Can you write Python code for it?

name = ['George HW', 'Bill', 'George', 'Barack', 'Donald', 'Bugs']
surname = ['Bush', 'Clinton', 'Bush Jr', 'Obama', 'Trump', 'Bunny']
full_names = {n:s for n,s in zip(name,surname)}
full_names

# What if we want to exclude certain values?
full_names = {n:s for n,s in zip(name, surname) if n!='Bugs'}
print(full_names)


I am working on the file "07.Loop_it_up.ipynb".
The first task is:
We can also use the range function to perform mathematical tricks.
Can you write Python code for it?

for i in range(1,6):
    print("The square of",i,"is:",i**2)


I am working on the file "07.Loop_it_up.ipynb".
The first task is:
When using dictionaries, you can iterate through keys, values or both.
Can you write Python code for it?

groceries = {"Milk":2.5, "Tea": 4, "Biscuits": 3.5, "Sugar":1}
print(groceries.keys())
print(groceries.values())

# item here refers to the the key in set name groceries
for a in groceries.keys():
    print(a)

for price in groceries.values():
    print(price)

for (key, val) in groceries.items():
    print(key,val)

groceries.items()

groceries.keys()

groceries.values()


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas (complete)-checkpoint.ipynb".
The first task is:
...but maybe we want to see more than a measly five results?
Can you write Python code for it?

# Let's look at MORE of the first few rows
df.head(10)


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas (complete)-checkpoint.ipynb".
The first task is:
A dataframe is basically a spreadsheet, except it lives in the world of Python or the statistical programming language R. They can't call it a spreadsheet because then people would think those programmers used Excel, which would make them boring and normal and they'd have to wear a tie every day.
Selecting rows
Now let's look at our data, since that's what data is for
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas (complete)-checkpoint.ipynb".
The first task is:
But that's ugly. There's a thing called ggplot for R that looks nice. We want to look nice. We want to look like ggplot.
Can you write Python code for it?

import matplotlib.pyplot as plt
plt.style.available

plt.style.use('ggplot')

df['Age'].hist()

plt.style.use('seaborn-deep')
df['Age'].hist()

plt.style.use('fivethirtyeight')
df['Age'].hist()


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas (complete)-checkpoint.ipynb".
The first task is:
I want more graphics! Do tall people make more money?!?!
Can you write Python code for it?

df.plot(kind='scatter', x='feet', y='millions')

df.head()

# How does experience relate with the amount of money they're making?
df.plot(kind='scatter', x='EXP', y='millions')

# At least we can assume height and weight are related
df.plot(kind='scatter', x='WT', y='feet')

# At least we can assume height and weight are related
# http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.html
df.plot(kind='scatter', x='WT', y='feet', xlim=(100,300), ylim=(5.5, 8))

plt.style.use('ggplot')

df.plot(kind='scatter', x='WT', y='feet', xlim=(100,300), ylim=(5.5, 8))

# We can also use plt separately
# It's SIMILAR but TOTALLY DIFFERENT
centers = df[df['POS'] == 'C']
guards = df[df['POS'] == 'G']
forwards = df[df['POS'] == 'F']
plt.scatter(y=centers["feet"], x=centers["WT"], c='c', alpha=0.75, marker='x')
plt.scatter(y=guards["feet"], x=guards["WT"], c='y', alpha=0.75, marker='o')
plt.scatter(y=forwards["feet"], x=forwards["WT"], c='m', alpha=0.75, marker='v')
plt.xlim(100,300)
plt.ylim(5.5,8)


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas (complete)-checkpoint.ipynb".
The first task is:
If we scroll we can see all of it. But maybe we don't want to see all of it. Maybe we hate scrolling?
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas (complete)-checkpoint.ipynb".
The first task is:
It's kind of like an array, right? Except where in an array we'd say df[0] this time we need to give it two numbers, the start and the end.
Selecting columns
But jeez, my eyes don't want to go that far over the data. I only want to see, uh, name and age.
Can you write Python code for it?

# Get the names of the columns, just because
df.columns

# If we want to be "correct" we add .values on the end of it
df.columns.values

# Select only name and age
columns_to_show = ['Name', 'Age']
df[columns_to_show]

# Combing that with .head() to see not-so-many rows
columns_to_show = ['Name', 'Age']
df[columns_to_show].head()

# We can also do this all in one line, even though it starts looking ugly
# (unlike the cute bears pandas looks ugly pretty often)
df[['Name', 'Age']].head()


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas (complete)-checkpoint.ipynb".
The first task is:
Now that was a little weird, yes - we used df['POS'] instead of df[['POS']] when viewing the data's details.
But now I'm curious about numbers: how old is everyone? Maybe we could, I don't know, get some statistics about age? Some statistics to describe age?
Can you write Python code for it?

# Summary statistics for Age
df['Age'].describe()

# That's pretty good. Does it work for everything? How about the money?
df['2013 $'].describe()


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas (complete)-checkpoint.ipynb".
The first task is:
Okay that was nice but unfortunately we can't do anything with it. It's just sitting there, separate from our data. If this were normal code we could do blahblah['feet'] = blahblah['Ht (In.)'] / 12, but since this is pandas, we can't. Right? Right?
Can you write Python code for it?

# Store a new column
df['feet'] = df['Ht (In.)'] / 12
df.head()


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas (complete)-checkpoint.ipynb".
The first task is:
So yes, head and tail work kind of like the terminal commands. That's nice, I guess.
But maybe we're incredibly demanding (which we are) and we want, say, the 6th through the 8th row (which we do). Don't worry (which I know you were), we can do that, too.
Can you write Python code for it?

# Show the 6th through the 8th rows
df[5:8]


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas (complete)-checkpoint.ipynb".
The first task is:
That might look better with a little more customization. So let's customize it.
Can you write Python code for it?

# Pass in all sorts of stuff!
# Most from http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.hist.html
# .range() is a matplotlib thing
df['Age'].hist(bins=20, xlabelsize=10, ylabelsize=10, range=(0,40))


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas (complete)-checkpoint.ipynb".
The first task is:
That's cool, maybe we could do the same thing with their salary? Take out the $ and the , and convert it to an integer?
Can you write Python code for it?

# Can't just use .replace
df['2013 $'].head().replace("$","")

# Need to use this weird .str thing
df['2013 $'].head().str.replace("$","")

# Can't just immediately replace the , either
df['2013 $'].head().str.replace("$","").replace(",","")

# Need to use the .str thing before EVERY string method
df['2013 $'].head().str.replace("$","").str.replace(",","")

# Describe still doesn't work.
df['2013 $'].head().str.replace("$","").str.replace(",","").describe()

# Let's convert it to an integer using .astype(int) before we describe it
df['2013 $'].head().str.replace("$","").str.replace(",","").astype(int).describe()

df['2013 $'].head().str.replace("$","").str.replace(",","").astype(int)

# Maybe we can just make them millions?
df['2013 $'].head().str.replace("$","").str.replace(",","").astype(int) / 1000000

# Unfortunately one is "n/a" which is going to break our code, so we can make n/a be 0
df['2013 $'].str.replace("$","").str.replace(",","").str.replace("n/a", "0").astype(int) / 1000000

# Remove the .head() piece and save it back into the dataframe
df['millions'] = df['2013 $'].str.replace("$","").str.replace(",","").str.replace("n/a","0").astype(int) / 1000000
df.head()

df.describe()


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas (complete)-checkpoint.ipynb".
The first task is:
That's stupid, though, what's an inch even look like? What's 80 inches? I don't have a clue. If only there were some wa to manipulate our data.
Manipulating data
Oh wait there is, HA HA HA.
Can you write Python code for it?

# Take another look at our inches, but only the first few
df['Ht (In.)'].head()

# Divide those inches by 12
df['Ht (In.)'].head() / 12

# Let's divide ALL of them by 12
feet = df['Ht (In.)'] / 12
feet

# Can we get statistics on those?
feet.describe()

# Let's look at our original data again
df.head(2)


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas (complete)-checkpoint.ipynb".
The first task is:
Those guys are making nothing! If only there were a way to sort from high to low, a.k.a. descending instead of ascending.
Can you write Python code for it?

# It isn't descending = True, unfortunately
df.sort_values(by='millions', ascending=False).head(3)

# We can use this to find the oldest guys in the league
df.sort_values(by='Age', ascending=False).head(3)

# Or the youngest, by taking out 'ascending=False'
df.sort_values(by='Age').head(3)


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas (complete)-checkpoint.ipynb".
The first task is:
Unfortunately because that has dollar signs and commas it's thought of as a string. We'll fix it in a second, but let's try describing one more thing.
Can you write Python code for it?

# Doing more describing
df['Ht (In.)'].describe()


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas-checkpoint.ipynb".
The first task is:
A dataframe is basically a spreadsheet, except it lives in the world of Python or the statistical programming language R. They can't call it a spreadsheet because then people would think those programmers used Excel, which would make them boring and normal and they'd have to wear a tie every day.
Selecting rows
Now let's look at our data, since that's what data is for
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas-checkpoint.ipynb".
The first task is:
But maybe we want to make a basketball joke and see the final four?
Can you write Python code for it?

# Let's look at the final few rows
df.tail(4)


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas-checkpoint.ipynb".
The first task is:
But sometimes instead of just looking at them, I want to do stuff with them. Play some games with them! Dunk on them~ describe them! And we don't want to dunk on everyone, only the players above 7 feet tall.
First, we need to check out boolean things.
Can you write Python code for it?

# Get a big long list of True and False for every single row.
df['feet'] > 6.5

# We could use value counts if we wanted
above_or_below_six_five = df['feet'] > 6.5
above_or_below_six_five.value_counts()

# But we can also apply this to every single row to say whether YES we want it or NO we don't

# Instead of putting column names inside of the brackets, we instead
# put the True/False statements. It will only return the players above
# seven feet tall

df[df['feet'] > 6.5]

df['Race'] == 'Asian'

df[]

# Or only the guards
df['POS'] == 'G'.head()
#People below 6 feet
df['feet'] < 6.5

#Every column you ant to query needs parenthesis aroung it
#Guards that are higher than 6.5
#this is combination of both
df[(df['POS'] == 'G') & (df['feet'] < 6.5)].head()

#We can save stuff
centers = df[df['POS'] == 'C']
guards = df[df['POS'] ==  'G']

centers['feet'].describe()

guards['feet'].describe()

# It might be easier to break down the booleans into separate variables

# We can save this stuff

# Maybe we can compare them to taller players?


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas-checkpoint.ipynb".
The first task is:
But that's ugly. There's a thing called ggplot for R that looks nice. We want to look nice. We want to look like ggplot.
Can you write Python code for it?

# Import matplotlib
# What's available?

# Use ggplot

# Make a histogram

# Try some other styles


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas-checkpoint.ipynb".
The first task is:
I want to know how many people are in each position. Luckily, pandas can tell me!
Can you write Python code for it?

# Grab the POS column, and count the different values in it.
df['POS'].value_counts()


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas-checkpoint.ipynb".
The first task is:
If we scroll we can see all of it. But maybe we don't want to see all of it. Maybe we hate scrolling?
Can you write Python code for it?

# Look at the first few rows
df.head() #shows first 5 rows


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas-checkpoint.ipynb".
The first task is:
NOTE: That was not df['Name', 'Age'], it was df[['Name', 'Age']]. You'll definitely type it wrong all of the time. When things break with pandas it's probably because you forgot to put in a million brackets.
Describing your data
A powerful tool of pandas is being able to select a portion of your data, because who ordered all that data anyway.
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas-checkpoint.ipynb".
The first task is:
So yes, head and tail work kind of like the terminal commands. That's nice, I guess.
But maybe we're incredibly demanding (which we are) and we want, say, the 6th through the 8th row (which we do). Don't worry (which I know you were), we can do that, too.
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas-checkpoint.ipynb".
The first task is:
That might look better with a little more customization. So let's customize it.
Can you write Python code for it?

# Pass in all sorts of stuff!
# Most from http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.hist.html
# .range() is a matplotlib thing


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas-checkpoint.ipynb".
The first task is:
That's stupid, though, what's an inch even look like? What's 80 inches? I don't have a clue. If only there were some wa to manipulate our data.
Manipulating data
Oh wait there is, HA HA HA.
Can you write Python code for it?

# Take another look at our inches, but only the first few
df['Ht (In.)'].head()

# Divide those inches by 12
#number_of_inches = 300
#number_of_inches / 12
df['Ht (In.)'].head() / 12

# Let's divide ALL of them by 12
df['Ht (In.)'] / 12

# Can we get statistics on those?
height_in_feet = df['Ht (In.)'] / 12
height_in_feet.describe()

# Let's look at our original data again
df.head(3)


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas-checkpoint.ipynb".
The first task is:
The average basketball player makes 3.8 million dollars and is a little over six and a half feet tall.
But who cares about those guys? I don't care about those guys. They're boring. I want the real rich guys!
Sorting and sub-selecting
Can you write Python code for it?

# This is just the first few guys in the dataset. Can we order it?

# Let's try to sort them, ascending value

df.sort_values('feet')


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas-checkpoint.ipynb".
The first task is:
Those guys are making nothing! If only there were a way to sort from high to low, a.k.a. descending instead of ascending.
Can you write Python code for it?

# It isn't descending = True, unfortunately
df.sort_values('feet', ascending=False).head()

# We can use this to find the oldest guys in the league
df.sort_values('Age', ascending=False).head()

# Or the youngest, by taking out 'ascending=False'
df.sort_values('feet').head()


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas-checkpoint.ipynb".
The first task is:
Unfortunately because that has dollar signs and commas it's thought of as a string. We'll fix it in a second, but let's try describing one more thing.
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas-checkpoint.ipynb".
The first task is:
When you import pandas, you use import pandas as pd. That means instead of typing pandas in your code you'll type pd.
You don't have to, but every other person on the planet will be doing it, so you might as well.
Now we're going to read in a file. Our file is called NBA-Census-10.14.2013.csv because we're sports moguls. pandas can read_ different types of files, so try to figure it out by typing pd.read_ and hitting tab for autocomplete.
Can you write Python code for it?

# We're going to call this df, which means "data frame"
# It isn't in UTF-8 (I saved it from my mac!) so we need to set the encoding
df = pd.read_csv("NBA-Census-10.14.2013.csv", encoding ="mac_roman")
#this is a data frame (df)


I am working on the file "07/.ipynb_checkpoints/07 - Introduction to Pandas-checkpoint.ipynb".
The first task is:
matplotlib is a graphing library. It's the Python way to make graphs!
Can you write Python code for it?

# this will open up a weird window that won't do anything

# So instead you run this code


I am working on the file "07/.ipynb_checkpoints/07_Introduction_to_Pandas-checkpoint.ipynb".
The first task is:
...but maybe we want to see more than a measly five results?
Can you write Python code for it?

# Let's look at MORE of the first few rows
df.head(10)


I am working on the file "07/.ipynb_checkpoints/07_Introduction_to_Pandas-checkpoint.ipynb".
The first task is:
But that's ugly. There's a thing called ggplot for R that looks nice. We want to look nice. We want to look like ggplot.
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/07_Introduction_to_Pandas-checkpoint.ipynb".
The first task is:
Drawing pictures
Okay okay enough code and enough stupid numbers. I'm visual. I want graphics. Okay????? Okay.
Can you write Python code for it?

!pip install matplotlib

# This will scream we don't have matplotlib.

df['feet'].hist()


I am working on the file "07/.ipynb_checkpoints/07_Introduction_to_Pandas-checkpoint.ipynb".
The first task is:
I want more graphics! Do tall people make more money?!?!
Can you write Python code for it?

# How does experience relate with the amount of money they're making?

# At least we can assume height and weight are related

# At least we can assume height and weight are related
# http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.html

# We can also use plt separately
# It's SIMILAR but TOTALLY DIFFERENT


I am working on the file "07/.ipynb_checkpoints/07_Introduction_to_Pandas-checkpoint.ipynb".
The first task is:
I want to know how many people are in each position. Luckily, pandas can tell me!
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/07_Introduction_to_Pandas-checkpoint.ipynb".
The first task is:
If we scroll we can see all of it. But maybe we don't want to see all of it. Maybe we hate scrolling?
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/07_Introduction_to_Pandas-checkpoint.ipynb".
The first task is:
Now that was a little weird, yes - we used df['POS'] instead of df[['POS']] when viewing the data's details.
But now I'm curious about numbers: how old is everyone? Maybe we could, I don't know, get some statistics about age? Some statistics to describe age?
Can you write Python code for it?

#race
race_counts = df['Race'].value_counts()
race_counts

# Summary statistics for Age
df['Age'].describe()

df.describe()

# That's pretty good. Does it work for everything? How about the money?
df['2013 $'].describe()
#The result is the result, because the Money is a string.


I am working on the file "07/.ipynb_checkpoints/07_Introduction_to_Pandas-checkpoint.ipynb".
The first task is:
Okay that was nice but unfortunately we can't do anything with it. It's just sitting there, separate from our data. If this were normal code we could do blahblah['feet'] = blahblah['Ht (In.)'] / 12, but since this is pandas, we can't. Right? Right?
Can you write Python code for it?

# Store a new column
df['feet'] = df['Ht (In.)'] / 12
df.head()


I am working on the file "07/.ipynb_checkpoints/07_Introduction_to_Pandas-checkpoint.ipynb".
The first task is:
So yes, head and tail work kind of like the terminal commands. That's nice, I guess.
But maybe we're incredibly demanding (which we are) and we want, say, the 6th through the 8th row (which we do). Don't worry (which I know you were), we can do that, too.
Can you write Python code for it?

# Show the 6th through the 8th rows
df[5:8]


I am working on the file "07/.ipynb_checkpoints/07_Introduction_to_Pandas-checkpoint.ipynb".
The first task is:
That's cool, maybe we could do the same thing with their salary? Take out the $ and the , and convert it to an integer?
Can you write Python code for it?

# Can't just use .replace

# Need to use this weird .str thing

# Can't just immediately replace the , either

# Need to use the .str thing before EVERY string method

# Describe still doesn't work.

# Let's convert it to an integer using .astype(int) before we describe it

# Maybe we can just make them millions?

# Unfortunately one is "n/a" which is going to break our code, so we can make n/a be 0

# Remove the .head() piece and save it back into the dataframe


I am working on the file "07/.ipynb_checkpoints/07_Introduction_to_Pandas-checkpoint.ipynb".
The first task is:
That's stupid, though, what's an inch even look like? What's 80 inches? I don't have a clue. If only there were some wa to manipulate our data.
Manipulating data
Oh wait there is, HA HA HA.
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/07_Introduction_to_Pandas-checkpoint.ipynb".
The first task is:
Those guys are making nothing! If only there were a way to sort from high to low, a.k.a. descending instead of ascending.
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/07_Introduction_to_Pandas-checkpoint.ipynb".
The first task is:
When you import pandas, you use import pandas as pd. That means instead of typing pandas in your code you'll type pd.
You don't have to, but every other person on the planet will be doing it, so you might as well.
Now we're going to read in a file. Our file is called NBA-Census-10.14.2013.csv because we're sports moguls. pandas can read_ different types of files, so try to figure it out by typing pd.read_ and hitting tab for autocomplete.
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/Animal_Panda_Homework_7_Skinner-checkpoint.ipynb".
The first task is:
*10. 'length' is the animal's length in cm. Create a new column called inches that is the length in inches.
Can you write Python code for it?

df['inches'] = df['length'] * 0.394
df.head()


I am working on the file "07/.ipynb_checkpoints/Animal_Panda_Homework_7_Skinner-checkpoint.ipynb".
The first task is:
*11. Save the cats to a separate variable called "cats." Save the dogs to a separate variable called "dogs."
Can you write Python code for it?

dogs = df[df['animal'] == 'dog']
cats = df[df['animal'] == 'cat']


I am working on the file "07/.ipynb_checkpoints/Animal_Panda_Homework_7_Skinner-checkpoint.ipynb".
The first task is:
*13. What's the mean length of a cat?
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/Animal_Panda_Homework_7_Skinner-checkpoint.ipynb".
The first task is:
*15. Use groupby to accomplish both of the above tasks at once.
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/Animal_Panda_Homework_7_Skinner-checkpoint.ipynb".
The first task is:
*16. Make a histogram of the length of dogs. I apologize that it is so boring.
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/Animal_Panda_Homework_7_Skinner-checkpoint.ipynb".
The first task is:
*17. Change your graphing style to be something else (anything else!)
Can you write Python code for it?

import matplotlib.pyplot as plt
plt.style.available

plt.style.use('ggplot')

dogs['inches'].hist()


I am working on the file "07/.ipynb_checkpoints/Animal_Panda_Homework_7_Skinner-checkpoint.ipynb".
The first task is:
*19. Make a sorted horizontal bar graph of the cats, with the larger cats on top.
Can you write Python code for it?

cats_sorted = cats.sort_values(by='length', ascending=True).head(3)
cats_sorted.plot(kind='barh', x='name', y='length', legend=False)


I am working on the file "07/.ipynb_checkpoints/Animal_Panda_Homework_7_Skinner-checkpoint.ipynb".
The first task is:
*3. Import pandas with the right name
Can you write Python code for it?

#for encoding the command would look smth like this:
#df = pd.read_csv("XXXXXXXXXXXXXXXXX.csv", encoding='mac_roman')
df = pd.read_csv("Animal_Data/07-hw-animals.csv")


I am working on the file "07/.ipynb_checkpoints/Animal_Panda_Homework_7_Skinner-checkpoint.ipynb".
The first task is:
*4. Display the names of the columns in the csv
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/Animal_Panda_Homework_7_Skinner-checkpoint.ipynb".
The first task is:
*5. Display the first 3 animals.
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/Animal_Panda_Homework_7_Skinner-checkpoint.ipynb".
The first task is:
*6. Sort the animals to see the 3 longest animals.
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/Animal_Panda_Homework_7_Skinner-checkpoint.ipynb".
The first task is:
*7. What are the counts of the different values of the "animal" column? a.k.a. how many cats and how many dogs.
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/Homework7-JOINS-checkpoint.ipynb".
The first task is:
2. Millionaires
What country are most billionaires from? For the top ones, how many billionaires per billion people?
Can you write Python code for it?

df2 = pd.read_excel("richpeople.xlsx")

df2.keys()

df2['citizenship'].value_counts().head(10)

# population: data from http://data.worldbank.org/indicator/SP.POP.TOTL
df_pop = pd.read_csv("world_pop.csv", header=2)

df_pop.keys()
#recent_pop = df_pop['2015']

#join: see http://pandas.pydata.org/pandas-docs/stable/merging.html#joining-on-index
left = pd.DataFrame(df2, index=['countrycode'])
right = pd.DataFrame(df_pop, index=['Country Code'])

millionaires_and_pop = left.join(right)
result = pd.merge(left, right, left_index=True, right_index=True, how='outer')

millionaires_and_pop
result
#millionaires_and_pop['citizenship'].value_counts().head(10)


I am working on the file "07/.ipynb_checkpoints/Homework7-JOINS-checkpoint.ipynb".
The first task is:
Given the richest person in a country, what % of the GDP is their wealth?
Can you write Python code for it?

#per_country = df2.groupby(['citizenship'])
#per_country['networthusbillion'].max()
#per_country['networthusbillion'].idxmax() # DataFrame.max(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)
# per_country['gdpcurrentus']

df2['percofgdp'] = (100*1000000000*df2['networthusbillion']) / (df2['gdpcurrentus'])

#pd.Series(["{0:.2f}%".format(percofgdp)])

print("NB: most countries doesn't have their GDP in the 'gdpcurrentus' column.")

df2.loc[per_country['networthusbillion'].idxmax()][['name', 'networthusbillion', 'percofgdp']]


I am working on the file "07/.ipynb_checkpoints/Homework7-JOINS-checkpoint.ipynb".
The first task is:
Trains stations
Can you write Python code for it?

df_trains = pd.read_csv("stations.csv", delimiter=';')
df_trains


I am working on the file "07/.ipynb_checkpoints/Homework7-JOINS-checkpoint.ipynb".
The first task is:
What is 'relationship to company'? And what are the most common relationships?
Can you write Python code for it?

#relationship_values = set
relationship_list = df2['relationshiptocompany'].tolist()
relationship_set = set(relationship_list)
relationship_set = [s.strip() for s in relationship_set if s == s] # to remove a naughty NaN and get rid of dumb whitespaces

print("The relationships are:", str.join(', ', relationship_set))

print('\nThe five most common relationships are:')

df2['relationshiptocompany'].value_counts().head(5)


I am working on the file "07/.ipynb_checkpoints/Homework7-JOINS-checkpoint.ipynb".
The first task is:
Who is the poorest billionaire? Who are the top 10 poorest billionaires?
Can you write Python code for it?

print('The poorest billionaire is:', df2.get_value(df2.sort_values('networthusbillion', ascending=True).index[0],'name'))

df2.sort_values('networthusbillion', ascending=True).head(10)


I am working on the file "07/.ipynb_checkpoints/Homework_7_Emelike-checkpoint.ipynb".
The first task is:
1) What country are most billionaires from? For the top ones, how many billionaires per billion people?
2) Who are the top 10 richest billionaires?
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/Homework_7_Emelike-checkpoint.ipynb".
The first task is:
12) Display all of the animals that are cats and above 12 inches long. First do it using the "cats" variable, then do it using your normal dataframe.
Can you write Python code for it?

cats[cats['inches']>12]

df[(df['animal']=='cat') & (df['inches']>12)]


I am working on the file "07/.ipynb_checkpoints/Homework_7_Emelike-checkpoint.ipynb".
The first task is:
13) What's the mean length of a cat?
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/Homework_7_Emelike-checkpoint.ipynb".
The first task is:
16) Make a histogram of the length of dogs. I apologize that it is so boring.
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/Homework_7_Emelike-checkpoint.ipynb".
The first task is:
17) Change your graphing style to be something else (anything else!)
Can you write Python code for it?

df.plot(kind='bar', x='name', y='length', legend=False)


I am working on the file "07/.ipynb_checkpoints/Homework_7_Emelike-checkpoint.ipynb".
The first task is:
18) Make a horizontal bar graph of the length of the animals, with their name as the label (look at the billionaires notebook I put on Slack!)
Can you write Python code for it?

df.plot(kind='barh', x='animal', y='length', legend=False)


I am working on the file "07/.ipynb_checkpoints/Homework_7_Emelike-checkpoint.ipynb".
The first task is:
19) Make a sorted horizontal bar graph of the cats, with the larger cats on top.
Can you write Python code for it?

sortedcats = cats.sort_values(by='length', ascending = True)

sortedcats.plot(kind='barh', x='animal', y='length', legend=False)


I am working on the file "07/.ipynb_checkpoints/Homework_7_Emelike-checkpoint.ipynb".
The first task is:
2) Set all graphics from matplotlib to display inline
Can you write Python code for it?

!pip install matplotlib
import matplotlib.pyplot as plt
%matplotlib inline


I am working on the file "07/.ipynb_checkpoints/Homework_7_Emelike-checkpoint.ipynb".
The first task is:
4) Display the names of the columns in the csv
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/Homework_7_Emelike-checkpoint.ipynb".
The first task is:
4) Who is the poorest billionaire? Who are the top 10 poorest billionaires?
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/Homework_7_Emelike-checkpoint.ipynb".
The first task is:
5) 'What is relationship to company'? And what are the most common relationships?
Can you write Python code for it?

rel_counts = recent.groupby('relationshiptocompany').count()
rel_counts.sort_values('year', ascending=False).head(10)
#relationship to company describes the role a person plays in a company
#most common relationshops are founder, relation, owner, chairman, and investor


I am working on the file "07/.ipynb_checkpoints/Homework_7_Emelike-checkpoint.ipynb".
The first task is:
5) Display the first 3 animals.
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/Homework_7_Emelike-checkpoint.ipynb".
The first task is:
6) Most common source of wealth? Male vs. female?
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/Homework_7_Emelike-checkpoint.ipynb".
The first task is:
6) Sort the animals to see the 3 longest animals.
Can you write Python code for it?

df.sort_values(by='length', ascending = False).head(3)


I am working on the file "07/.ipynb_checkpoints/Homework_7_Emelike-checkpoint.ipynb".
The first task is:
7) What are the counts of the different values of the "animal" column? a.k.a. how many cats and how many dogs.
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/Homework_7_Emelike-checkpoint.ipynb".
The first task is:
8) Only select the dogs.
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/Homework_7_Emelike-checkpoint.ipynb".
The first task is:
9) Display all of the animals that are greater than 40 cm.
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/Homework_7_Emelike-checkpoint.ipynb".
The first task is:
the mean length of a cat is 14.698 inches
14) What's the mean length of a dog?
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/Homework_7_Emelike-checkpoint.ipynb".
The first task is:
the mean length of a dog is 19.685
15) Use groupby to accomplish both of the above tasks at once.
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/billionaires-checkpoint.ipynb".
The first task is:
Add up the wealth of all of the billionaires in a given country (or a few countries) and then compare it to the GDP of the country, or other billionaires, so like pit the US vs India
What are the most common industries for billionaires to come from? What's the total amount of billionaire money from each industry?
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/billionaires-checkpoint.ipynb".
The first task is:
Given the richest person in a country, what % of the GDP is their wealth?
Can you write Python code for it?

richest = df[df['citizenship'] == 'United States'].sort_values('rank').head(1)['networthusbillion'].to_dict()
# richest['networthusbillion']
richest[282]

## I JUST WANT THE VALUE -- 18.5.

## 16.77 TRILLION
US_GDP = 1.677 * (10^13)
US_GDP


I am working on the file "07/.ipynb_checkpoints/billionaires-checkpoint.ipynb".
The first task is:
How many self made billionaires vs. others?
Can you write Python code for it?


I am working on the file "07/.ipynb_checkpoints/billionaires-checkpoint.ipynb".
The first task is:
How old are billionaires self made vs. non self made?
Can you write Python code for it?

df.groupby('selfmade')['age'].mean()

 # or different industries?

df.groupby('sector')['age'].mean()

#youngest billionnaires
recent.sort_values('age').head(10)

#oldest billionnaires
recent.sort_values('age', ascending =False).head(10)

#Age distribution - maybe make a graph about it?

import matplotlib.pyplot as plt
%matplotlib inline
# This will scream we don't have matplotlib.
his = df['age'].hist(range=[0, 100])

his.set_title('Distribution of Age Amongst Billionaires')
his.set_xlabel('Age(years)')
his.set_ylabel('# of Billionnaires')

# Maybe just made a graph about how wealthy they are in general?

import matplotlib.pyplot as plt
%matplotlib inline
# This will scream we don't have matplotlib.

his = df['networthusbillion'].hist(range=[0, 45])

his.set_title('Distribution of Wealth Amongst Billionaires')
his.set_xlabel('Wealth(Billions)')
his.set_ylabel('# of Billionnaires')


I am working on the file "07/.ipynb_checkpoints/billionaires-checkpoint.ipynb".
The first task is:
How old are billionaires?
Can you write Python code for it?

# recent['age'].value_counts().sort_values()
print("The average billionnaire is", round(recent['age'].mean()), "years old.")


I am working on the file "07/.ipynb_checkpoints/billionaires-checkpoint.ipynb".
The first task is:
Maybe plot their net worth vs age (scatterplot)
Make a bar graph of the top 10 or 20 richest
Can you write Python code for it?

recent.plot(kind='scatter', x='networthusbillion', y='age')

recent.plot(kind='scatter', x='age', y='networthusbillion')


I am working on the file "07/.ipynb_checkpoints/billionaires-checkpoint.ipynb".
The first task is:
Who are the top 10 richest billionaires?
Can you write Python code for it?

recent = df[df['year'] == 2014]
# if it is not recent then there are duplicates for diff years
recent.sort_values('rank').head(10)


I am working on the file "07/.ipynb_checkpoints/billionaires-checkpoint.ipynb".
The first task is:
Who is the poorest billionaire?
Can you write Python code for it?

recent.sort_values('networthusbillion').head(1)

# Who are the top 10 poorest billionaires?

# Who are the top 10 poorest billionaires

recent.sort_values('networthusbillion').head(10)

# 'What is relationship to company'? And what are the most common relationships?

#top 10 most common relationships to company
df['relationshiptocompany'].value_counts().head(10)

# Most common source of wealth? Male vs. female?

# Most common source of wealth? Male vs. female

print("The most common source of wealth is", df['sourceofwealth'].value_counts().head(1))
print("The most common source of wealth for males is", male['sourceofwealth'].value_counts().head(1))
print("The most common source of wealth for females is", female['sourceofwealth'].value_counts().head(1))

#need to figure out how to extract just the number nd not the data type 'Name: sourceofwealth, dtype: int64'


I am working on the file "07/07 Homework Part 2.ipynb".
The first task is:
'What is relationship to company'? And what are the most common relationships?
Can you write Python code for it?


I am working on the file "07/07 Homework Part 2.ipynb".
The first task is:
Add up the wealth of all of the billionaires in a given country (or a few countries) and then compare it to the GDP of the country, or other billionaires, so like pit the US vs India
Can you write Python code for it?


I am working on the file "07/07 Homework Part 2.ipynb".
The first task is:
Given the richest person in a country, what % of the GDP is their wealth?
Can you write Python code for it?

gdp_csv=pd.read_csv('http://data.okfn.org/data/core/gdp/r/gdp.csv')
gdp_2014 = gdp_csv[gdp_csv['Year'] == 2014]
gdp_2014.tail()
gdp_2014=gdp_2014.rename(columns={'Country Code': 'countrycode'})
gdp_2014.tail()
rich2=pd.merge(rich1, gdp_2014, on='countrycode', how='outer')
rich2['percentageofcountrygdptheirwealthis']=(rich2['networthusbillion'])/(rich2['Value'])
rich2['percentageofcountrygdptheirwealthis'].head()


I am working on the file "07/07 Homework Part 2.ipynb".
The first task is:
How many self made billionaires vs. others?
Can you write Python code for it?


I am working on the file "07/07 Homework Part 2.ipynb".
The first task is:
How old are billionaires? How old are billionaires self made vs. non self made? or different industries?
Can you write Python code for it?

selfmade=rich2[rich2['selfmade']=='self-made']
nonselfmade=rich2[rich2['selfmade']=='inherited']
print("The values for self made billionaires")
selfmade['age'].value_counts()
selfmade['age'].hist()

print("The values for non-self made billionaires")
nonselfmade['age'].value_counts()
nonselfmade['age'].hist(label='Self Made Billionaires Age vs Number')

rich2['age'].value_counts()


I am working on the file "07/07 Homework Part 2.ipynb".
The first task is:
What are the most common industries for billionaires to come from? What's the total amount of billionaire money from each industry?
Can you write Python code for it?


I am working on the file "07/07 Homework Part 2.ipynb".
The first task is:
What's the average wealth of a billionaire? Male? Female?
Can you write Python code for it?

df['networthusbillion'].describe()

maleb['networthusbillion'].describe()

femaleb['networthusbillion'].describe()


I am working on the file "07/07 Homework Part 2.ipynb".
The first task is:
Who are the top 10 richest billionaires?
Can you write Python code for it?


I am working on the file "07/07 Homework Part 2.ipynb".
The first task is:
Who are the youngest billionaires? The oldest?
Can you write Python code for it?


I am working on the file "07/07 Homework Part 2.ipynb".
The first task is:
Who is the poorest billionaire? Who are the top 10 poorest billionaires?
Can you write Python code for it?


I am working on the file "07/Animal_Panda_Homework_7_Skinner.ipynb".
The first task is:
*10. 'length' is the animal's length in cm. Create a new column called inches that is the length in inches.
Can you write Python code for it?

df['inches'] = df['length'] * 0.394
df.head()


I am working on the file "07/Animal_Panda_Homework_7_Skinner.ipynb".
The first task is:
*11. Save the cats to a separate variable called "cats." Save the dogs to a separate variable called "dogs."
Can you write Python code for it?

dogs = df[df['animal'] == 'dog']
cats = df[df['animal'] == 'cat']


I am working on the file "07/Animal_Panda_Homework_7_Skinner.ipynb".
The first task is:
*12. Display all of the animals that are cats and above 12 inches long. First do it using the "cats" variable, then do it using your normal dataframe.
Can you write Python code for it?

cats[cats['inches'] > 12]

#df[df[df[df['animal'] == 'cat']'inches'] > 12]
#df[df['animal'] == 'cat']&
#df[df['inches'] > 12]

#pd.read_csv('imdb.txt')
#  .sort(columns='year')
#  .filter('year >1990')
#  .to_csv('filtered.csv')

df[(df['animal'] == 'cat') & (df['inches'] > 12)]

#3 > 2 & 4 > 3
#true  & true
#true

#3 > 2 & 4 > 3
#true  & 4 > 3

#(3 > 2) & (4 > 3)


I am working on the file "07/Animal_Panda_Homework_7_Skinner.ipynb".
The first task is:
*13. What's the mean length of a cat?
Can you write Python code for it?


I am working on the file "07/Animal_Panda_Homework_7_Skinner.ipynb".
The first task is:
*14. What's the mean length of a dog?
Can you write Python code for it?


I am working on the file "07/Animal_Panda_Homework_7_Skinner.ipynb".
The first task is:
*15. Use groupby to accomplish both of the above tasks at once.
Can you write Python code for it?


I am working on the file "07/Animal_Panda_Homework_7_Skinner.ipynb".
The first task is:
*16. Make a histogram of the length of dogs. I apologize that it is so boring.
Can you write Python code for it?


I am working on the file "07/Animal_Panda_Homework_7_Skinner.ipynb".
The first task is:
*17. Change your graphing style to be something else (anything else!)
Can you write Python code for it?

import matplotlib.pyplot as plt
plt.style.available

plt.style.use('ggplot')

dogs['inches'].hist()


I am working on the file "07/Animal_Panda_Homework_7_Skinner.ipynb".
The first task is:
*18. Make a horizontal bar graph of the length of the animals, with their name as the label (look at the billionaires notebook I put on Slack!)
Can you write Python code for it?

df['length'].plot(kind='bar')

#or:

df.plot(kind='barh', x='name', y='length', legend=False)


I am working on the file "07/Animal_Panda_Homework_7_Skinner.ipynb".
The first task is:
*2. Set all graphics from matplotlib to display inline
Can you write Python code for it?

import matplotlib.pyplot as plt
%matplotlib inline


I am working on the file "07/Animal_Panda_Homework_7_Skinner.ipynb".
The first task is:
*3. Import pandas with the right name
Can you write Python code for it?

#for encoding the command would look smth like this:
#df = pd.read_csv("XXXXXXXXXXXXXXXXX.csv", encoding='mac_roman')
df = pd.read_csv("Animal_Data/07-hw-animals.csv")


I am working on the file "07/Animal_Panda_Homework_7_Skinner.ipynb".
The first task is:
*4. Display the names of the columns in the csv
Can you write Python code for it?


I am working on the file "07/Animal_Panda_Homework_7_Skinner.ipynb".
The first task is:
*5. Display the first 3 animals.
Can you write Python code for it?


I am working on the file "07/Animal_Panda_Homework_7_Skinner.ipynb".
The first task is:
*8. Only select the dogs.
Can you write Python code for it?

#df['animal'] == 'dog' this just tests, whether row is a dog or not, True or False
#is_dog = df['animal'] == 'dog'
#df[is_dog]
df[df['animal'] == 'dog']


I am working on the file "07/Animal_Panda_Homework_7_Skinner.ipynb".
The first task is:
*9. Display all of the animals that are greater than 40 cm.
Can you write Python code for it?


I am working on the file "07/Class-7.ipynb".
The first task is:
A dataframe is basically a spreadsheet, except it lives in the world of Python or the statistical programming language R. They can't call it a spreadsheet because then people would think those programmers used Excel, which would make them boring and normal and they'd have to wear a tie every day.
Selecting rows
Now let's look at our data, since that's what data is for
Can you write Python code for it?


I am working on the file "07/Class-7.ipynb".
The first task is:
But maybe we want to make a basketball joke and see the final four?
Can you write Python code for it?

# Let's look at the final few rows
df.tail(4)


I am working on the file "07/Class-7.ipynb".
The first task is:
But sometimes instead of just looking at them, I want to do stuff with them. Play some games with them! Dunk on them~ describe them! And we don't want to dunk on everyone, only the players above 7 feet tall.
First, we need to check out boolean things.
Can you write Python code for it?

# Get a big long list of True and False for every single row.
df['feet'] > 7

# We could use value counts if we wanted
above_seven_feet = df['feet'] > 7
above_seven_feet.value_counts()

# But we can also apply this to every single row to say whether YES we want it or NO we don't
df['feet'].head() > 7

# Instead of putting column names inside of the brackets, we instead
# put the True/False statements. It will only return the players above
# seven feet tall
df[df['feet'] > 7]

# Or only the guards
df[df['POS'] == 'G']

# Or only the guards who make more than 15 million
df[(df['POS'] == 'G') & (df['millions'] > 15)]

# It might be easier to break down the booleans into separate variables
is_guard = df['POS'] == 'G'
more_than_fifteen_million = df['millions'] > 15
df[is_guard & more_than_fifteen_million]

# We can save this stuff
short_players = df[df['feet'] < 6.5]
short_players

short_players.describe()

# Maybe we can compare them to taller players?
df[df['feet'] >= 6.5].describe()


I am working on the file "07/Class-7.ipynb".
The first task is:
But that's ugly. There's a thing called ggplot for R that looks nice. We want to look nice. We want to look like ggplot.
Can you write Python code for it?

import matplotlib.pyplot as plt
plt.style.available

plt.style.use('ggplot')

df['Age'].hist()

plt.style.use('seaborn-deep')
df['Age'].hist()

plt.style.use('fivethirtyeight')
df['Age'].hist()


I am working on the file "07/Class-7.ipynb".
The first task is:
Now that was a little weird, yes - we used df['POS'] instead of df[['POS']] when viewing the data's details.
But now I'm curious about numbers: how old is everyone? Maybe we could, I don't know, get some statistics about age? Some statistics to describe age?
Can you write Python code for it?

# Summary statistics for Age
df['Age'].describe()

# That's pretty good. Does it work for everything? How about the money?
df['2013 $'].describe()


I am working on the file "07/Class-7.ipynb".
The first task is:
Okay that was nice but unfortunately we can't do anything with it. It's just sitting there, separate from our data. If this were normal code we could do blahblah['feet'] = blahblah['Ht (In.)'] / 12, but since this is pandas, we can't. Right? Right?
Can you write Python code for it?

# Store a new column
df['feet'] = df['Ht (In.)'] / 12
df.head()


I am working on the file "07/Class-7.ipynb".
The first task is:
That might look better with a little more customization. So let's customize it.
Can you write Python code for it?

# Pass in all sorts of stuff!
# Most from http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.hist.html
# .range() is a matplotlib thing
df['Age'].hist(bins=20, xlabelsize=10, ylabelsize=10, range=(0,40))


I am working on the file "07/Class-7.ipynb".
The first task is:
That's cool, maybe we could do the same thing with their salary? Take out the $ and the , and convert it to an integer?
Can you write Python code for it?

# Can't just use .replace
df['2013 $'].head().replace("$","")

# Need to use this weird .str thing
df['2013 $'].head().str.replace("$","")

# Can't just immediately replace the , either
df['2013 $'].head().str.replace("$","").replace(",","")

# Need to use the .str thing before EVERY string method
df['2013 $'].head().str.replace("$","").str.replace(",","")

# Describe still doesn't work.
df['2013 $'].head().str.replace("$","").str.replace(",","").describe()

# Let's convert it to an integer using .astype(int) before we describe it
df['2013 $'].head().str.replace("$","").str.replace(",","").astype(int).describe()

df['2013 $'].head().str.replace("$","").str.replace(",","").astype(int)

# Maybe we can just make them millions?
df['2013 $'].head().str.replace("$","").str.replace(",","").astype(int) / 1000000

# Unfortunately one is "n/a" which is going to break our code, so we can make n/a be 0
df['2013 $'].str.replace("$","").str.replace(",","").str.replace("n/a", "0").astype(int) / 1000000

# Remove the .head() piece and save it back into the dataframe
df['millions'] = df['2013 $'].str.replace("$","").str.replace(",","").str.replace("n/a","0").astype(int) / 1000000
df.head()

df.describe()


I am working on the file "07/Class-7.ipynb".
The first task is:
The average basketball player makes 3.8 million dollars and is a little over six and a half feet tall.
But who cares about those guys? I don't care about those guys. They're boring. I want the real rich guys!
Sorting and sub-selecting
Can you write Python code for it?

# This is just the first few guys in the dataset. Can we order it?
df.head(3)

# Let's try to sort them
df.sort_values(by='millions').head(3)


I am working on the file "07/Class-7.ipynb".
The first task is:
Unfortunately because that has dollar signs and commas it's thought of as a string. We'll fix it in a second, but let's try describing one more thing.
Can you write Python code for it?

# Doing more describing
df['Ht (In.)'].describe()


I am working on the file "07/Class-7.ipynb".
The first task is:
When you import pandas, you use import pandas as pd. That means instead of typing pandas in your code you'll type pd.
You don't have to, but every other person on the planet will be doing it, so you might as well.
Now we're going to read in a file. Our file is called NBA-Census-10.14.2013.csv because we're sports moguls. pandas can read_ different types of files, so try to figure it out by typing pd.read_ and hitting tab for autocomplete.
Can you write Python code for it?

# We're going to call this df, which means "data frame"
# It isn't in UTF-8 (I saved it from my mac!) so we need to set the encoding
df = pd.read_csv("NBA-Census-10.14.2013.csv", encoding='mac_roman')


I am working on the file "07/Homework7.ipynb".
The first task is:
3 Train stations
Can you write Python code for it?

df_trains = pd.read_csv("stations.csv", delimiter=';')
print('A dataset about train stations and their accessibility in Switzerland:')
df_trains


I am working on the file "07/Homework7.ipynb".
The first task is:
Given the richest person in a country, what % of the GDP is their wealth?
Can you write Python code for it?

per_country = df2.groupby(['citizenship'])
#per_country['networthusbillion'].max()
#per_country['networthusbillion'].idxmax() # DataFrame.max(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)
# per_country['gdpcurrentus']

df2['percofgdp'] = (100*1000000000*df2['networthusbillion']) / (df2['gdpcurrentus'])

#pd.Series(["{0:.2f}%".format(percofgdp)])

print("NB: most countries doesn't have their GDP in the 'gdpcurrentus' column.")

df2.loc[per_country['networthusbillion'].idxmax()][['name', 'networthusbillion', 'percofgdp']]


I am working on the file "07/Homework7.ipynb".
The first task is:
What's the average wealth of a billionaire? Male? Female?
Can you write Python code for it?

print("The average wealth of a billionaire (in billions) is:", df2['networthusbillion'].describe()['mean'])

print("The average wealth of a male billionaire is:", df2[df2['gender'] == 'male']['networthusbillion'].describe()['mean'])

print("The average wealth of a female billionaire is:", df2[df2['gender'] == 'female']['networthusbillion'].describe()['mean'])


I am working on the file "07/Homework7.ipynb".
The first task is:
Who is the poorest billionaire? Who are the top 10 poorest billionaires?
Can you write Python code for it?

print('The poorest billionaire is:', df2.get_value(df2.sort_values('networthusbillion', ascending=True).index[0],'name'))

df2.sort_values('networthusbillion', ascending=True).head(10)


I am working on the file "07/RichPeople_Pandas_Homework_7_Skinner.ipynb".
The first task is:
1) Where are most billionaires from?
Can you write Python code for it?


I am working on the file "07/RichPeople_Pandas_Homework_7_Skinner.ipynb".
The first task is:
11. What about the youngest billionaires. Are any of them female?
Can you write Python code for it?


I am working on the file "07/RichPeople_Pandas_Homework_7_Skinner.ipynb".
The first task is:
2) How many of them are from Switzerland?
Can you write Python code for it?

Swiss_billionaires = recent[recent['countrycode'] == 'CHE']
Swiss_billionaires.count().head(1)


I am working on the file "07/RichPeople_Pandas_Homework_7_Skinner.ipynb".
The first task is:
4) How many of the richest Swiss are female, and how many are male?
Can you write Python code for it?

female_Swiss_billionaires = Swiss_billionaires[Swiss_billionaires['gender'] == 'female'].count().head(1)
female_Swiss_billionaires

male_Swiss_billionaire = Swiss_billionaires[Swiss_billionaires['gender'] == 'male'].count().head(1)
male_Swiss_billionaire


I am working on the file "07/RichPeople_Pandas_Homework_7_Skinner.ipynb".
The first task is:
6) Do more Swiss women or more men inherit their fortunes?
Can you write Python code for it?

#df[(df['animal'] == 'cat') & (df['inches'] > 12)]
male_Swiss_billionaires_list[male_Swiss_billionaires_list['selfmade'] == 'inherited'].count().head(1)
inherited = male_Swiss_billionaires_list[male_Swiss_billionaires_list['selfmade'] == 'inherited'].count().head(1)
print("So out of 14 male Swiss billionaires 9 of them inherited their fortune.")

female_Swiss_billionaires_list[female_Swiss_billionaires_list['selfmade'] == 'inherited'].count().head(1)
female_inherited = female_Swiss_billionaires_list[female_Swiss_billionaires_list['selfmade'] == 'inherited'].count().head()
female_inherited
#print("Out of 8 Swiss female billionaires, only one woman made her fortune on her own")


I am working on the file "07/RichPeople_Pandas_Homework_7_Skinner.ipynb".
The first task is:
Interesting. Chan Laiwa, the richest self made woman in the world is from China. She is also above 70. The second on the list, Gayle Cook, is even over 80.
10. Lets compare that to the richest self made men. How much have they made?
Can you write Python code for it?

recent[(recent['gender'] == 'male') & (recent['selfmade'] == 'self-made')].sort_values(by='networthusbillion', ascending=False)


I am working on the file "07/RichPeople_Pandas_Homework_7_Skinner.ipynb".
The first task is:
So that is 8 female Swiss billionaires and 14 male ones.
5) How is their wealth distributed? How many billions do super rich men and women have in Switzerland compared to women?
Can you write Python code for it?

female_Swiss_billionaires_list = Swiss_billionaires[Swiss_billionaires['gender'] == 'female']
female_mean = female_Swiss_billionaires_list['networthusbillion'].mean()
print(female_mean)

#print("Swiss female billionaire's average wealth:", female_Swiss_billionaire['networthusbillion'].mean())
#print("Swiss male billionaire's average wealth:", male_Swiss_billionaire['networthusbillion'].mean())

male_Swiss_billionaires_list = Swiss_billionaires[Swiss_billionaires['gender'] == 'male']
male_mean = male_Swiss_billionaires_list['networthusbillion'].mean()
print(male_mean)

diff = male_mean - female_mean
Millions_diff = diff * 1000
male_sum = male_Swiss_billionaires_list['networthusbillion'].sum()
female_sum = female_Swiss_billionaires_list['networthusbillion'].sum()

print("In total Swiss male billionaires have funds of", male_sum, "billion US-Dollars. Female billionaires own", female_sum,"billion meaning that in average a Swiss male billionaire has", Millions_diff, "million USD more than Swiss female billionaires. That's qite a bit of cash!")


I am working on the file "07/billionaires.ipynb".
The first task is:
Add up the wealth of all of the billionaires in a given country (or a few countries) and then compare it to the GDP of the country, or other billionaires, so like pit the US vs India
What are the most common industries for billionaires to come from? What's the total amount of billionaire money from each industry?
Can you write Python code for it?


I am working on the file "07/billionaires.ipynb".
The first task is:
Given the richest person in a country, what % of the GDP is their wealth?
Can you write Python code for it?

richest = df[df['citizenship'] == 'United States'].sort_values('rank').head(1)['networthusbillion'].to_dict()
# richest['networthusbillion']
richest[282]

## I JUST WANT THE VALUE -- 18.5.

## 16.77 TRILLION
US_GDP = 1.677 * (10^13)
US_GDP


I am working on the file "07/billionaires.ipynb".
The first task is:
How old are billionaires self made vs. non self made?
Can you write Python code for it?

df.groupby('selfmade')['age'].mean()

 # or different industries?

df.groupby('sector')['age'].mean()

#youngest billionnaires
recent.sort_values('age').head(10)

#oldest billionnaires
recent.sort_values('age', ascending =False).head(10)

#Age distribution - maybe make a graph about it?

import matplotlib.pyplot as plt
%matplotlib inline
# This will scream we don't have matplotlib.
his = df['age'].hist(range=[0, 100])

his.set_title('Distribution of Age Amongst Billionaires')
his.set_xlabel('Age(years)')
his.set_ylabel('# of Billionnaires')

# Maybe just made a graph about how wealthy they are in general?

import matplotlib.pyplot as plt
%matplotlib inline
# This will scream we don't have matplotlib.

his = df['networthusbillion'].hist(range=[0, 45])

his.set_title('Distribution of Wealth Amongst Billionaires')
his.set_xlabel('Wealth(Billions)')
his.set_ylabel('# of Billionnaires')


I am working on the file "07/billionaires.ipynb".
The first task is:
How old are billionaires?
Can you write Python code for it?

# recent['age'].value_counts().sort_values()
print("The average billionnaire is", round(recent['age'].mean()), "years old.")


I am working on the file "07/billionaires.ipynb".
The first task is:
What country are most billionaires from? For the top ones, how many billionaires per billion people?
Can you write Python code for it?

df['citizenship'].value_counts().head()

df.groupby('citizenship')['networthusbillion'].sum().sort_values(ascending=False)

us_pop = 318.9 #billion (2014)
us_bill = df[df['citizenship'] == 'United States']
print("There are",  us_pop/len(us_bill), "billionaires per billion people in the United States.")

germ_pop = 0.08062 #(2013)
germ_bill = df[df['citizenship'] == 'Germany']
print("There are",  germ_pop/len(germ_bill), "billionaires per billion people in Germany.")

china_pop = 1.357 #(2013)
china_bill = df[df['citizenship'] == 'China']
print("There are",  china_pop/len(china_bill), "billionaires per billion people in China.")

russia_pop = 0.1435 #(2013)
russia_bill = df[df['citizenship'] == 'Russia']
print("There are",  russia_pop/len(russia_bill), "billionaires per billion people in Russia.")

japan_pop = 0.1273 # 2013
japan_bill = df[df['citizenship'] == 'Japan']
print("There are",  japan_pop/len(japan_bill), "billionaires per billion people in Japan.")

print(df.columns)


I am working on the file "07/billionaires.ipynb".
The first task is:
Who are the top 10 richest billionaires?
Can you write Python code for it?

recent = df[df['year'] == 2014]
# if it is not recent then there are duplicates for diff years
recent.sort_values('rank').head(10)

recent['networthusbillion'].describe()


I am working on the file "07/billionaires.ipynb".
The first task is:
Who is the poorest billionaire?
Can you write Python code for it?

recent.sort_values('networthusbillion').head(1)

# Who are the top 10 poorest billionaires?

# Who are the top 10 poorest billionaires

recent.sort_values('networthusbillion').head(10)

# 'What is relationship to company'? And what are the most common relationships?

#top 10 most common relationships to company
df['relationshiptocompany'].value_counts().head(10)

# Most common source of wealth? Male vs. female?

# Most common source of wealth? Male vs. female

print("The most common source of wealth is", df['sourceofwealth'].value_counts().head(1))
print("The most common source of wealth for males is", male['sourceofwealth'].value_counts().head(1))
print("The most common source of wealth for females is", female['sourceofwealth'].value_counts().head(1))

#need to figure out how to extract just the number nd not the data type 'Name: sourceofwealth, dtype: int64'


I am working on the file "07/homework-07-part2.ipynb".
The first task is:
'What is relationship to company'? And what are the most common relationships?
Can you write Python code for it?

# relationship to company looks like job title for the most part. looks like you want to get in
# on the ground floor to make those B's
df['relationshiptocompany'].value_counts().head(10)


I am working on the file "07/homework-07-part2.ipynb".
The first task is:
Add up the wealth of all of the billionaires in a given country (or a few countries) and then compare it to the GDP of the country, or other billionaires, so like pit the US vs India
Can you write Python code for it?

total_worth=pd.DataFrame(networth_gdp.groupby('Country')['networthusbillion'].sum())

total_worth['total_worth']=total_worth['networthusbillion']*1000000000

total_worth.index

total_worth['Country']=total_worth.index

total_worth=pd.merge(total_worth, gdp, on='Country', how='left')

total_worth['GDP (millions USD)']=total_worth['GDP (millions USD)']*1000000

total_worth['GDP']=total_worth['GDP (millions USD)']

del total_worth['GDP (millions USD)']

total_worth['percent_by_billionaires']=total_worth['total_worth']/total_worth['GDP']

# here is the top ten countries by percent of gdp possessed by billionaires
total_worth[['Country', 'percent_by_billionaires']].sort_values(by='percent_by_billionaires', ascending=False).head(10)

# and now us vs india
total_worth[['Country', 'percent_by_billionaires']].loc[total_worth['Country'].isin(['India', 'United States'])]


I am working on the file "07/homework-07-part2.ipynb".
The first task is:
Maybe plot their net worth vs age (scatterplot)
Can you write Python code for it?

# comparing a number to itself to get rid of NaNs is a trick i learned from stackoverflow
# also this isnt a very good graph. nearly all of the billionaires at any age have 1 or near to 1 billion
df[['age', 'networthusbillion']].loc[df['age'] == df['age']].plot(kind='scatter', x='age', y='networthusbillion')


I am working on the file "07/homework-07-part2.ipynb".
The first task is:
What are the most common industries for billionaires to come from? What's the total amount of billionaire money from each industry?
Can you write Python code for it?


I am working on the file "07/homework-07-part2.ipynb".
The first task is:
What country are most billionaires from? For the top ones, how many billionaires per billion people?
Can you write Python code for it?

df=df[df['year']==2014]

df.columns

countries=df.groupby('countrycode')['countrycode'].count().sort_values(ascending=False).head(10)
countries_df=pd.DataFrame(countries, columns=['countrycode'])
countries_df.rename(columns = {'countrycode':'count'}, inplace = True)

countries_df

# this part im just looking up and manually adding
# i could have probably learned census.govs terrible api to automate this but for 10 countrie this was faster
countries_df['pop_thousands']=[323996, 1373541, 142355, 80723, 205824, 1266884, 64430, 7167, 66836, 62008]
countries_df['billionaires_per_billion']=countries_df['count']/(countries_df['pop_thousands']/100000)

countries_df

# this is my beautiful bargraph. hong kong clearly has it going on
countries_df.sort_values(by='billionaires_per_billion', ascending=True).plot(kind='barh', y='billionaires_per_billion', title='Billionaires Per Billion Persons', legend=False)


I am working on the file "07/homework-07-part2.ipynb".
The first task is:
What's the average wealth of a billionaire? Male? Female?
Can you write Python code for it?

df['networthusbillion'].describe()

df[['networthusbillion', 'gender']].groupby('gender').describe()


I am working on the file "07/homework-07-part2.ipynb".
The first task is:
Who are the top 10 richest billionaires?
Can you write Python code for it?

df.columns

df[['name', 'networthusbillion']].sort_values(by='networthusbillion', ascending=False).head(10)


I am working on the file "07/homework-07-part2.ipynb".
The first task is:
Who is the poorest billionaire? Who are the top 10 poorest billionaires?
Can you write Python code for it?

df[['name', 'networthusbillion']].sort_values(by='networthusbillion', ascending=True).head(10)

# actually there are a lot more than 10 tied for poorest billionaires. here is the whole list!
df[df['networthusbillion']==1]['name']

# 81 tied for poorest. 102 tied for second place.
df['networthusbillion'].value_counts().sort_index().head(10)

df.columns


I am working on the file "07/homework-7-schuetz.ipynb".
The first task is:
'length' is the animal's length in cm. Create a new column called inches that is the length in inches.
Can you write Python code for it?

inches = df['length'] * 0.393701
df['inches'] = inches


I am working on the file "07/homework-7-schuetz.ipynb".
The first task is:
02: Doing some research
Answer your own selection out of the following questions, or any other questions you might be able to think of.  Write the question down first in a markdown cell (use a # to make the question a nice header), THEN try to get an answer to it. A lot of these are remarkably similar, and some you'll need to do manual work for - the GDP ones, for example.

If you are trying to figure out some other question that we didn't cover in class and it does not have to do with joining to another data set, we're happy to help you figure it out during lab!

Take a peek at the billionaires notebook I uploaded into Slack; it should be helpful for the graphs (I added a few other styles and options, too). You'll probably also want to look at the "sum()" line I added.

## What country are most billionaires from? For the top ones, how many billionaires per billion people?
Can you write Python code for it?

df = pd.read_excel('richpeople.xlsx')
df = df[df['year'] == 2014]


I am working on the file "07/homework-7-schuetz.ipynb".
The first task is:
03: Finding your own dataset
On Thursday, bring a dataset with you that's a csv/tsv/whatever. Try to open it in pandas, and do df.head() to make sure it displays OK.
Can you write Python code for it?

df = pd.read_json('https://data.sfgov.org/api/views/gxxq-x39z/rows.json')
# Can't get this to work! And I can't save the source code for some reason.

df.head()


I am working on the file "07/homework-7-schuetz.ipynb".
The first task is:
But There Are Many More People Who Make Just As Little Money
Can you write Python code for it?

poorest_billionaires = df[(df['networthusbillion']) == (df['networthusbillion'].sort_values().head(1).values[0])]
print('But there are', poorest_billionaires['name'].count(), 'billionaires making just as little money:')
print('')
print(poorest_billionaires[['name', 'rank', 'networthusbillion']])


I am working on the file "07/homework-7-schuetz.ipynb".
The first task is:
Display all of the animals that are greater than 40 cm.
Can you write Python code for it?


I am working on the file "07/homework-7-schuetz.ipynb".
The first task is:
Display the first 3 animals.
Can you write Python code for it?


I am working on the file "07/homework-7-schuetz.ipynb".
The first task is:
Display the names of the columns in the csv
Can you write Python code for it?


I am working on the file "07/homework-7-schuetz.ipynb".
The first task is:
How old are billionaires? How old are billionaires self made vs. non self made? or different industries?
Billionaire Ages
Can you write Python code for it?


I am working on the file "07/homework-7-schuetz.ipynb".
The first task is:
Make a histogram of the length of dogs. I apologize that it is so boring.
Can you write Python code for it?


I am working on the file "07/homework-7-schuetz.ipynb".
The first task is:
Make a sorted horizontal bar graph of the cats, with the larger cats on top.
Can you write Python code for it?

cats.sort_values('inches').plot(kind = 'barh', x = 'name', y = 'inches', legend=False)


I am working on the file "07/homework-7-schuetz.ipynb".
The first task is:
Maybe just made a graph about how wealthy they are in general?
Can you write Python code for it?

df['networthusbillion'].hist()

# df['networthusbillion'].sort_values(ascending = False).head(10)


I am working on the file "07/homework-7-schuetz.ipynb".
The first task is:
Maybe plot their net worth vs age (scatterplot)
Can you write Python code for it?

df[['networthusbillion', 'age']].plot(kind = 'scatter', x = 'networthusbillion', y = 'age')


I am working on the file "07/homework-7-schuetz.ipynb".
The first task is:
Most common source of wealth? Male vs. female?
Can you write Python code for it?

print('Most common source of wealth:')
df['sourceofwealth'].value_counts().head(1)

print('The most common source of wealth for females and males:')

df[['gender', 'sourceofwealth']].groupby('gender').describe()


I am working on the file "07/homework-7-schuetz.ipynb".
The first task is:
Self-Made Billionaire Ages
Can you write Python code for it?

df[df['selfmade'] == 'self-made']['age'].hist()

df[df['selfmade'] == 'self-made']['age'].describe()


I am working on the file "07/homework-7-schuetz.ipynb".
The first task is:
The Ages of Billionaires in Different Industries
Can you write Python code for it?


I am working on the file "07/homework-7-schuetz.ipynb".
The first task is:
Use groupby to accomplish both of the above tasks at once.
Can you write Python code for it?


I am working on the file "07/homework-7-schuetz.ipynb".
The first task is:
What are the counts of the different values of the "animal" column? a.k.a. how many cats and how many dogs.
Can you write Python code for it?


I am working on the file "07/homework-7-schuetz.ipynb".
The first task is:
What are the most common industries for billionaires to come from? What's the total amount of billionaire money from each industry?
Can you write Python code for it?

# df.columns

# df[['networthusbillion', 'industry', 'sector']].head()

print('The most common industries for billionaires to come from:')
df['industry'].value_counts().head()

print('The total amount of billionaire money in each industry:')
df[['industry', 'networthusbillion']].groupby('industry').sum().sort_values('networthusbillion', ascending = False)


I am working on the file "07/homework-7-schuetz.ipynb".
The first task is:
What's the average wealth of a billionaire? Male? Female?
Can you write Python code for it?


I am working on the file "07/homework-7-schuetz.ipynb".
The first task is:
What's the mean length of a cat?
Can you write Python code for it?


I am working on the file "07/homework-7-schuetz.ipynb".
The first task is:
Who are the top 10 richest billionaires?
Can you write Python code for it?

df[['name', 'rank', 'networthusbillion']].sort_values('networthusbillion', ascending = False).head(10)


I am working on the file "07/homework-7-schuetz.ipynb".
The first task is:
Who are the youngest billionaires? The oldest? Age distribution - maybe make a graph about it?
The Youngest Billionaires
Can you write Python code for it?


I am working on the file "07/homework-7-schuetz.ipynb".
The first task is:
Who is the poorest billionaire? Who are the top 10 poorest billionaires?
The 'Top 10' Poorest
Can you write Python code for it?


I am working on the file "07/pandas-homework-hon-june13.ipynb".
The first task is:
Change your graphing style to be something else (anything else!)
Can you write Python code for it?


I am working on the file "07/pandas-homework-hon-june13.ipynb".
The first task is:
Display all of the animals that are cats and above 12 inches long. First do it using the "cats" variable, then do it using your normal dataframe.
Can you write Python code for it?

long_animals = df['length_inches'] > 12
df[cats & long_animals]

df[(df['length_inches'] > 12) & (df['animal'] == 'cat')]
#Amazing!


I am working on the file "07/pandas-homework-hon-june13.ipynb".
The first task is:
Display the names of the columns in the csv
Can you write Python code for it?


I am working on the file "07/pandas-homework-hon-june13.ipynb".
The first task is:
Make a histogram of the length of dogs. I apologize that it is so boring.
Can you write Python code for it?


I am working on the file "07/pandas-homework-hon-june13.ipynb".
The first task is:
Make a horizontal bar graph of the length of the animals, with their name as the label (look at the billionaires notebook I put on Slack!)
Can you write Python code for it?

df[dogs].plot.barh(x='name', y='length_inches')
#Fontaine is such an annoying name for a dog


I am working on the file "07/pandas-homework-hon-june13.ipynb".
The first task is:
Save the cats to a separate variable called "cats." Save the dogs to a separate variable called "dogs."
Can you write Python code for it?

cats = df['animal'] == 'cat'
dogs = df['animal'] == 'dog'


I am working on the file "07/pandas-homework-hon-june13.ipynb".
The first task is:
Set all graphics from matplotlib to display inline
Can you write Python code for it?

#!pip install matplotlib

import matplotlib.pyplot as plt
%matplotlib inline
#This lets your graph show you in your notebook

df


I am working on the file "07/pandas-homework-hon-june13.ipynb".
The first task is:
Sort the animals to see the 3 longest animals.
Can you write Python code for it?


I am working on the file "07/pandas-homework-hon-june13.ipynb".
The first task is:
Use groupby to accomplish both of the above tasks at once.
Can you write Python code for it?


I am working on the file "07/pandas_cheatsheet.ipynb".
The first task is:
'length' is the animal's length in cm. Create a new column called inches that is the length in inches.
Can you write Python code for it?

length_in = df['length']* 0.3937

df['length (in.)'] = length_in


I am working on the file "07/pandas_cheatsheet.ipynb".
The first task is:
Change your graphing style to be something else (anything else!)
Can you write Python code for it?

df.plot(kind="bar", x="name", y="length", color = "red", legend =False)

df.plot(kind="barh", x="name", y="length", color = "red", legend =False)

dogs

dogs.plot(kind='bar')

# dogs.plot(kind='scatter', x='name', y='length (in.)')


I am working on the file "07/pandas_cheatsheet.ipynb".
The first task is:
Display all of the animals that are cats and above 12 inches long. First do it using the "cats" variable, then do it using your normal dataframe.
Can you write Python code for it?

cats['length'] > 12

df[(df['length'] > 12) & (df['animal'] == 'cat')]


I am working on the file "07/pandas_cheatsheet.ipynb".
The first task is:
Display the first 3 animals.
Can you write Python code for it?

df.head(3)

# Sort the animals to see the 3 longest animals.

df.sort_values('length', ascending = False).head(3)

# What are the counts of the different values of the "animal" column? a.k.a. how many cats and how many dogs.
# Only select the dogs.

(df['animal'] == 'dog').value_counts()

# Display all of the animals that are greater than 40 cm.

df[df['length'] > 40]


I am working on the file "07/pandas_cheatsheet.ipynb".
The first task is:
Make a histogram of the length of dogs. I apologize that it is so boring.
Can you write Python code for it?

dogs.plot(kind='hist', y = 'length (in.)') # all the same length "/


I am working on the file "07/pandas_cheatsheet.ipynb".
The first task is:
Make a horizontal bar graph of the length of the animals, with their name as the label
Can you write Python code for it?

df.columns

dogs['name']

dogs.plot(kind='bar', x='name', y = 'length', legend=False)


I am working on the file "07/pandas_cheatsheet.ipynb".
The first task is:
Make a sorted horizontal bar graph of the cats, with the larger cats on top.
Can you write Python code for it?

cats.sort_values('length').plot(kind='barh', x='name', y = 'length', legend = False)


I am working on the file "07/pandas_cheatsheet.ipynb".
The first task is:
Read the csv in (it should be UTF-8 already so you don't have to worry about encoding), save it with the proper boring name
Can you write Python code for it?

df = pd.read_csv('07-hw-animals.csv')

df

# Display the names of the columns in the csv

df.columns


I am working on the file "07/pandas_cheatsheet.ipynb".
The first task is:
Save the cats to a separate variable called "cats." Save the dogs to a separate variable called "dogs."
Can you write Python code for it?

dogs = df[df['animal'] == 'dog']
cats = df[df['animal'] == 'cat']


I am working on the file "07/pandas_cheatsheet.ipynb".
The first task is:
Use groupby to accomplish both of the above tasks at once.
Can you write Python code for it?


I am working on the file "07/pandas_cheatsheet.ipynb".
The first task is:
What's the mean length of a cat?
Can you write Python code for it?

# cats.describe() displays all stats for length

cats['length'].mean()

#only shows mean length
cats.mean()


I am working on the file "07/pandas_cheatsheet.ipynb".
The first task is:
What's the mean length of a dog?
Can you write Python code for it?

dogs['length'].mean()

dogs['length'].describe()

dogs.mean()


I am working on the file "07/データベース.ipynb".
The first task is:
63. オブジェクトを値に格納したKVS
KVSを用い，アーティスト名（name）からタグと被タグ数（タグ付けされた回数）のリストを検索するためのデータベースを構築せよ．
さらに，ここで構築したデータベースを用い，アーティスト名からタグと被タグ数を検索せよ．
Can you write Python code for it?

import pickle
import plyvel
names = artists.name
tags = artists.tags
artist_tag_db = plyvel.DB('artist_tag.ldb', create_if_missing=True)
try:
    for name, tag in zip(names, tags):
        dump_tag = pickle.dumps(tag)
        artist_tag_db.put(name.encode('utf-8'), dump_tag)
except:
    artist_tag_db.close()
artist_tag_db.close()

artist_tag_db = plyvel.DB('artist_tag.ldb')
print(pickle.loads(artist_tag_db.get('Shabba Ranks'.encode('utf-8'))))
artist_tag_db.close()


I am working on the file "07/データベース.ipynb".
The first task is:
64. MongoDBの構築
アーティスト情報（artist.json.gz）をデータベースに登録せよ．
さらに，次のフィールドでインデックスを作成せよ:
- name
- aliases.name
- tags.value
- rating.value
Can you write Python code for it?

from pymongo import MongoClient
import json
with open('artist.json', 'r') as f:
    for i in f:

import pandas as pd
import pickle
with open('artists.pickle', 'rb') as f:
    dataframe = pickle.load(f)

names = dataframe['name'].fillna('')
allases = dataframe['allases'].fillna('')


I am working on the file "0707 비즈니스 밸류 창출 프로세스_2/1/(1) NLTK.ipynb".
The first task is:
Collection library

https://docs.python.org/3/library/collections.html#collections.Counter.most_common
Can you write Python code for it?

import collections # token으로 나눠진 데이터를 딕셔너리 형태로 변환 + 편리한 함수를 제공하는 library
import numpy as np
import pandas as pd

import matplotlib.pyplot as plt # 결과를 시각화 하기 위한 matplotlib
%matplotlib inline

# token - frequency 형태로 변환 : collections.Counter 함수!
print(collections.Counter(gutenberg_stemmed_tagged).most_common(50))

# tuple 형태로 저장되어 있는 데이터를 token, frequency로 나눠서 저장
token_list = []
freq_list = []
for token, freq in collections.Counter(gutenberg_stemmed_tagged).most_common(10):
    token_list.append(token)
    freq_list.append(freq)

print(token_list[:4])
print(freq_list[:4])

# list로 나눈 데이터를 pandas 형태로 저장
data = pd.concat([pd.DataFrame(token_list),pd.DataFrame(freq_list)], axis=1)
data.columns = ['word','tag','freq']
data.head()

# word index 대신 word를 보여주는 그래프
freqdist = nltk.FreqDist(gutenberg_lemma_tagged)
freqdist.plot(50)
freqdist.plot(50,cumulative=True)