<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Exercise from Think Stats, 2nd Edition (thinkstats2.com)<br>
Allen Downey
Step1:  Print value counts for <tt>birthord</tt> and compare to results published in the codebook
Step2:  Print value counts for <tt>prglngth</tt> and compare to results published in the codebook
Step3:  Print value counts for <tt>agepreg</tt> and compare to results published in the codebook.
Looking at this data, please remember my comments in the book about the obligation to approach data with consideration for the context and respect for the respondents.
Step4:  Compute the mean birthweight.
Step5:  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.
Step6:  Look through the codebook and find a variable, other than the ones mentioned in the book, that you find interesting.  Compute values counts, means, or other statistics.
Step7:  Create a boolean Series.
Step8:  Use a boolean Series to select the records for the pregnancies that ended in live birth.
Step9:  Count the number of live births with <tt>birthwgt_lb</tt> between 0 and 5 pounds (including both).  The result should be 1125.
Step10:  Count the number of live births with <tt>birthwgt_lb</tt> between 9 and 95 pounds (including both).  The result should be 798
Step11:  Use <tt>birthord</tt> to select the records for first babies and others.  How many are there of each?
Step12:  Compute the mean weight for first babies and others.
Step13:  Compute the mean <tt>prglngth</tt> for first babies and others.  Compute the difference in means, expressed in hours.
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
import nsfg
df = nsfg.ReadFemPreg()
df
Explanation: Exercise from Think Stats, 2nd Edition (thinkstats2.com)<br>
Allen Downey
End of explanation
df.birthord.value_counts().sort_index()
Explanation: Print value counts for <tt>birthord</tt> and compare to results published in the codebook
End of explanation
df.prglngth.value_counts().sort_index()
Explanation: Print value counts for <tt>prglngth</tt> and compare to results published in the codebook
End of explanation
df.agepreg.hist(by=df.birthord)
Explanation: Print value counts for <tt>agepreg</tt> and compare to results published in the codebook.
Looking at this data, please remember my comments in the book about the obligation to approach data with consideration for the context and respect for the respondents.
End of explanation
df.totalwgt_lb.mean()
Explanation: Compute the mean birthweight.
End of explanation
df['totalwgt_kg'] = df.totalwgt_lb / 2.20462
df.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
df.finalwgt.value_counts().sort_index()
Explanation: Look through the codebook and find a variable, other than the ones mentioned in the book, that you find interesting.  Compute values counts, means, or other statistics.
End of explanation
df.outcome == 1
Explanation: Create a boolean Series.
End of explanation
live = df[df.outcome == 1]
len(live)
Explanation: Use a boolean Series to select the records for the pregnancies that ended in live birth.
End of explanation
len(live[(live.birthwgt_lb >= 0) & (live.birthwgt_lb <= 5)])
Explanation: Count the number of live births with <tt>birthwgt_lb</tt> between 0 and 5 pounds (including both).  The result should be 1125.
End of explanation
len(live[(live.birthwgt_lb >= 9) & (live.birthwgt_lb <= 95)])
Explanation: Count the number of live births with <tt>birthwgt_lb</tt> between 9 and 95 pounds (including both).  The result should be 798
End of explanation
firsts = df[df.birthord==1]
others = df[df.birthord>1]
len(firsts), len(others)
Explanation: Use <tt>birthord</tt> to select the records for first babies and others.  How many are there of each?
End of explanation
firsts.totalwgt_lb.mean()
others.totalwgt_lb.mean()
Explanation: Compute the mean weight for first babies and others.
End of explanation
fln = firsts.prglngth.mean()
oln = others.prglngth.mean()
hdiff = (fln - oln) * (7*24)
print "First baby's average pregnancy weeks: %.2f" % fln
print "Not first baby's average pregnancy weeks: %.2f" % oln
print "Average hours of difference: %.2f" % hdiff
Explanation: Compute the mean <tt>prglngth</tt> for first babies and others.  Compute the difference in means, expressed in hours.
End of explanation
<END_TASK>