In [35]:
import nsfg
df = nsfg.ReadFemPreg()
df
Out[35]:
Print value counts for birthord and compare to results published in the codebook
In [36]:
df.birthord.value_counts().sort_index()
Out[36]:
Print value counts for prglngth and compare to results published in the codebook
In [36]:
Print value counts for agepreg 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.
In [36]:
Compute the mean birthweight.
In [37]:
df.totalwgt_lb.mean()
Out[37]:
Create a new column named totalwgt_kg 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.
In [37]:
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.
In [37]:
Create a boolean Series.
In [38]:
df.outcome == 1
Out[38]:
Use a boolean Series to select the records for the pregnancies that ended in live birth.
In [39]:
live = df[df.outcome == 1]
len(live)
Out[39]:
Count the number of live births with birthwgt_lb between 0 and 5 pounds (including both). The result should be 1125.
In [40]:
len(live[(live.birthwgt_lb >= 0) & (live.birthwgt_lb <= 5)])
Out[40]:
Count the number of live births with birthwgt_lb between 9 and 95 pounds (including both). The result should be 798
In [40]:
Use birthord to select the records for first babies and others. How many are there of each?
In [41]:
firsts = df[df.birthord==1]
others = df[df.birthord>1]
len(firsts), len(others)
Out[41]:
Compute the mean weight for first babies and others.
In [42]:
firsts.totalwgt_lb.mean()
Out[42]:
In [43]:
others.totalwgt_lb.mean()
Out[43]:
Compute the mean prglngth for first babies and others. Compute the difference in means, expressed in hours.
In [43]: