<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Chapter 1
Examples and Exercises from Think Stats, 2nd Edition
http
Step1:  Examples from Chapter 1
Read NSFG data into a Pandas DataFrame.
Step2:  Print the column names.
Step3:  Select a single column name.
Step4:  Select a column and check what type it is.
Step5:  Print a column.
Step6:  Select a single element from a column.
Step7:  Select a slice from a column.
Step8:  Select a column using dot notation.
Step9:  Count the number of times each value occurs.
Step10:  Check the values of another variable.
Step11:  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.
Step12:  Exercises
Select the birthord column, print the value counts, and compare to results published in the codebook
Step13:  We can also use isnull to count the number of nans.
Step14:  Select the prglngth column, print the value counts, and compare to results published in the codebook
Step15:  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
Step16:  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.
Step17:  nsfg.py also provides ReadFemResp, which reads the female respondents file and returns a DataFrame
Step18:  DataFrame provides a method head that displays the first five rows
Step19:  Select the age_r column from resp and print the value counts.  How old are the youngest and oldest respondents?
Step20:  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
Step21:  And we can get the corresponding rows from preg like this
Step22:  How old is the respondent with caseid 1?
Step23:  What are the pregnancy lengths for the respondent with caseid 2298?
Step24:  What was the birthweight of the first baby born to the respondent with caseid 5012?
<ASSISTANT_TASK:>
Python Code:
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
# Solution
preg.birthord.value_counts().sort_index()
Explanation: Exercises
Select the birthord column, print the value counts, and compare to results published in the codebook
End of explanation
preg.birthord.isnull().sum()
Explanation: We can also use isnull to count the number of nans.
End of explanation
# Solution
preg.prglngth.value_counts().sort_index()
Explanation: Select the prglngth column, print the value counts, and compare to results published in the codebook
End of explanation
preg.totalwgt_lb.mean()
Explanation: 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
# Solution
preg['totalwgt_kg'] = preg.totalwgt_lb / 2.2
preg.totalwgt_kg.mean()
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.
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: 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
# Solution
resp.age_r.value_counts().sort_index()
Explanation: Select the age_r column from resp and print the value counts.  How old are the youngest and oldest respondents?
End of explanation
resp[resp.caseid==2298]
Explanation: 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
# Solution
resp[resp.caseid==1].age_r
Explanation: How old is the respondent with caseid 1?
End of explanation
# Solution
preg[preg.caseid==2298].prglngth
Explanation: What are the pregnancy lengths for the respondent with caseid 2298?
End of explanation
# Solution
preg[preg.caseid==5012].birthwgt_lb
Explanation: What was the birthweight of the first baby born to the respondent with caseid 5012?
End of explanation
<END_TASK>