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
"""


print df.agepreg.value_counts().sort_index()


"""
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 * 0.453592
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
"""


# FMAROUT5 - Formal marital status at pregnancy outcome
%matplotlib inline
print df.fmarout5.plot(kind='hist')

"""
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
"""


(df.outcome == 1).sum()

live = df[df.outcome == 1]
len(live)

"""
Explanation: Use a boolean Series to select the records for the pregnancies that ended in live birth.
Atleast two methods are there. sum() or len() can be used.
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
"""


live_births = df[df.outcome == 1]
len(live_births[(live_births.birthwgt_lb >= 9) & (live_births.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
"""


(firsts.prglngth.mean() - others.prglngth.mean())*7*24

"""
Explanation: Compute the mean <tt>prglngth</tt> for first babies and others.  Compute the difference in means, expressed in hours.
End of explanation
"""


