<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
We can also use isnull to count the number of nans.
Step13:  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
Step14:  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
Step15:  DataFrame provides a method head that displays the first five rows
Step16:  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
Step17:  And we can get the corresponding rows from preg like this
<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
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
<END_TASK>