from os.path import basename, exists


def download(url):
    filename = basename(url)
    if not exists(filename):
        from urllib.request import urlretrieve

        local, _ = urlretrieve(url, filename)
        print("Downloaded " + local)


download("https://github.com/AllenDowney/ThinkStats2/raw/master/code/thinkstats2.py")
download("https://github.com/AllenDowney/ThinkStats2/raw/master/code/thinkplot.py")

download("https://github.com/AllenDowney/ThinkStats2/raw/master/code/nsfg.py")

download("https://github.com/AllenDowney/ThinkStats2/raw/master/code/2002FemPreg.dct")
download(
    "https://github.com/AllenDowney/ThinkStats2/raw/master/code/2002FemPreg.dat.gz"
)

"""
Explanation: Chapter 1
Examples and Exercises from Think Stats, 2nd Edition
http://thinkstats2.com
Copyright 2016 Allen B. Downey
MIT License: https://opensource.org/licenses/MIT
End of explanation
"""


import nsfg

preg = nsfg.ReadFemPreg()
preg.head()

"""
Explanation: Examples from Chapter 1
Read NSFG data into a Pandas DataFrame.
End of explanation
"""


preg.columns

"""
Explanation: Print the column names.
End of explanation
"""


preg.columns[1]

"""
Explanation: Select a single column name.
End of explanation
"""


pregordr = preg['pregordr']
type(pregordr)

"""
Explanation: Select a column and check what type it is.
End of explanation
"""


pregordr

"""
Explanation: Print a column.
End of explanation
"""


pregordr[0]

"""
Explanation: Select a single element from a column.
End of explanation
"""


pregordr[2:5]

"""
Explanation: Select a slice from a column.
End of explanation
"""


pregordr = preg.pregordr

"""
Explanation: Select a column using dot notation.
End of explanation
"""


preg.outcome.value_counts().sort_index()

"""
Explanation: Count the number of times each value occurs.
End of explanation
"""


preg.birthwgt_lb.value_counts().sort_index()

"""
Explanation: Check the values of another variable.
End of explanation
"""


caseid = 10229
preg_map = nsfg.MakePregMap(preg)
indices = preg_map[caseid]
preg.outcome[indices].values

"""
Explanation: Make a dictionary that maps from each respondent's caseid to a list of indices into the pregnancy DataFrame.  Use it to select the pregnancy outcomes for a single respondent.
End of explanation
"""


preg.birthord.isnull().sum()

"""
Explanation: Exercises
Select the birthord column, print the value counts, and compare to results published in the codebook
We can also use isnull to count the number of nans.
End of explanation
"""


preg.totalwgt_lb.mean()

"""
Explanation: Select the prglngth column, print the value counts, and compare to results published in the codebook
To compute the mean of a column, you can invoke the mean method on a Series.  For example, here is the mean birthweight in pounds:
End of explanation
"""


download("https://github.com/AllenDowney/ThinkStats2/raw/master/code/2002FemResp.dct")
download("https://github.com/AllenDowney/ThinkStats2/raw/master/code/2002FemResp.dat.gz")

resp = nsfg.ReadFemResp()

"""
Explanation: Create a new column named <tt>totalwgt_kg</tt> that contains birth weight in kilograms.  Compute its mean.  Remember that when you create a new column, you have to use dictionary syntax, not dot notation.
nsfg.py also provides ReadFemResp, which reads the female respondents file and returns a DataFrame:
End of explanation
"""


resp.head()

"""
Explanation: DataFrame provides a method head that displays the first five rows:
End of explanation
"""


resp[resp.caseid==2298]

"""
Explanation: Select the age_r column from resp and print the value counts.  How old are the youngest and oldest respondents?
We can use the caseid to match up rows from resp and preg.  For example, we can select the row from resp for caseid 2298 like this:
End of explanation
"""


preg[preg.caseid==2298]

"""
Explanation: And we can get the corresponding rows from preg like this:
End of explanation
"""


