Exercise from Think Stats, 2nd Edition (thinkstats2.com)
Allen Downey


In [7]:
#!/usr/bin/python
#-*- encoding: utf-8 -*-

import sys
sys.path.append('./code')
import pandas as pd
import nsfg
import chap01ex as ex1


df = nsfg.ReadFemPreg()
#print(df.describe())


# Show All Rows
def printf(rows):
    pd.set_option('display.max_rows', len(rows))
    print(rows)
    pd.reset_option('display.max_rows')


# Usage of printf
    # printf(df.birthord)     # print all rows
    # printf(pregord[0:400])  # you can use slices
df


Out[7]:
caseid pregordr howpreg_n howpreg_p moscurrp nowprgdk pregend1 pregend2 nbrnaliv multbrth ... laborfor_i religion_i metro_i basewgt adj_mod_basewgt finalwgt secu_p sest cmintvw totalwgt_lb
0 1 1 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 3410.389399 3869.349602 6448.271112 2 9 NaN 8.8125
1 1 2 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 3410.389399 3869.349602 6448.271112 2 9 NaN 7.8750
2 2 1 NaN NaN NaN NaN 5 NaN 3 5 ... 0 0 0 7226.301740 8567.549110 12999.542264 2 12 NaN 9.1250
3 2 2 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 7226.301740 8567.549110 12999.542264 2 12 NaN 7.0000
4 2 3 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 7226.301740 8567.549110 12999.542264 2 12 NaN 6.1875
5 6 1 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 4870.926435 5325.196999 8874.440799 1 23 NaN 8.5625
6 6 2 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 4870.926435 5325.196999 8874.440799 1 23 NaN 9.5625
7 6 3 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 4870.926435 5325.196999 8874.440799 1 23 NaN 8.3750
8 7 1 NaN NaN NaN NaN 5 NaN 1 NaN ... 0 0 0 3409.579565 3787.539000 6911.879921 2 14 NaN 7.5625
9 7 2 NaN NaN NaN NaN 5 NaN 1 NaN ... 0 0 0 3409.579565 3787.539000 6911.879921 2 14 NaN 6.6250
10 12 1 NaN NaN NaN NaN 5 NaN 1 NaN ... 0 0 0 3612.781968 4146.013572 6909.331618 1 31 NaN 7.8125
11 14 1 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 2418.069494 2810.302771 3039.904507 2 56 NaN 7.0000
12 14 2 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 2418.069494 2810.302771 3039.904507 2 56 NaN 4.0000
13 14 3 NaN NaN NaN NaN 3 NaN NaN NaN ... 0 0 0 2418.069494 2810.302771 3039.904507 2 56 NaN NaN
14 15 1 NaN NaN NaN NaN 1 NaN NaN NaN ... 0 0 0 1667.816099 3200.862017 5553.495599 1 33 NaN NaN
15 15 2 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 1667.816099 3200.862017 5553.495599 1 33 NaN 7.6875
16 15 3 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 1667.816099 3200.862017 5553.495599 1 33 NaN 7.5000
17 18 1 NaN NaN NaN NaN 5 NaN 1 NaN ... 0 0 0 2957.257457 3404.403067 4153.371741 2 14 NaN 6.3125
18 18 2 NaN NaN NaN NaN 1 NaN NaN NaN ... 0 0 0 2957.257457 3404.403067 4153.371741 2 14 NaN NaN
19 21 1 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 3408.342437 3965.763949 7237.122630 1 48 NaN 8.7500
20 21 2 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 3408.342437 3965.763949 7237.122630 1 48 NaN 8.1875
21 23 1 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 6210.373020 8120.841310 13533.382043 2 64 NaN 5.5625
22 23 2 NaN NaN NaN NaN 1 NaN NaN NaN ... 0 0 0 6210.373020 8120.841310 13533.382043 2 64 NaN NaN
23 24 1 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 3409.573258 4068.628645 7424.840414 1 27 NaN 6.7500
24 24 2 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 3409.573258 4068.628645 7424.840414 1 27 NaN 7.3750
25 24 3 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 3409.573258 4068.628645 7424.840414 1 27 NaN 6.8125
26 28 1 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 3407.794208 3808.343516 6949.846082 2 57 NaN 8.1250
27 31 1 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 3405.679025 4272.084519 5211.943113 1 2 NaN 7.1250
28 31 2 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 3405.679025 4272.084519 5211.943113 1 2 NaN 6.0625
29 31 3 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 3405.679025 4272.084519 5211.943113 1 2 NaN 7.4375
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
13563 12547 2 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 3453.545517 6628.022524 11499.619080 1 52 NaN 7.6875
13564 12547 3 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 3453.545517 6628.022524 11499.619080 1 52 NaN 7.6250
13565 12550 1 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 3080.452699 3745.326058 5268.550165 1 79 NaN 8.1250
13566 12551 1 NaN NaN NaN NaN 5 NaN 1 NaN ... 0 0 0 2418.538866 3653.453268 3951.940400 2 75 NaN 7.5000
13567 12554 1 NaN NaN NaN NaN 3 NaN NaN NaN ... 0 0 0 1914.676604 2177.957240 2764.045534 2 75 NaN NaN
13568 12554 2 NaN NaN NaN NaN 4 NaN NaN NaN ... 0 0 0 1914.676604 2177.957240 2764.045534 2 75 NaN NaN
13569 12556 1 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 2474.619764 3250.573384 3965.699528 1 44 NaN 5.8125
13570 12556 2 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 2474.619764 3250.573384 3965.699528 1 44 NaN 6.6875
13571 12556 3 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 2474.619764 3250.573384 3965.699528 1 44 NaN 6.0000
13572 12556 4 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 2474.619764 3250.573384 3965.699528 1 44 NaN 5.8125
13573 12561 1 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 2418.089703 2698.650781 4497.301527 1 10 NaN 6.5625
13574 12561 2 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 2418.089703 2698.650781 4497.301527 1 10 NaN 6.1250
13575 12564 1 NaN NaN NaN NaN 3 NaN NaN NaN ... 0 0 0 1820.850938 2129.214067 2768.191208 2 44 NaN NaN
13576 12565 1 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 3195.641221 3834.241709 6652.409365 1 78 NaN 6.4375
13577 12565 2 35 1 8 NaN NaN NaN NaN NaN ... 0 0 0 3195.641221 3834.241709 6652.409365 1 78 NaN NaN
13578 12566 1 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 2080.317155 2422.820274 2627.548587 2 2 NaN 6.0000
13579 12566 2 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 2080.317155 2422.820274 2627.548587 2 2 NaN 7.0000
13580 12568 1 NaN NaN NaN NaN 1 NaN NaN NaN ... 0 0 0 2734.687353 4258.980140 7772.212858 2 28 NaN NaN
13581 12568 2 NaN NaN NaN NaN 5 NaN 1 NaN ... 0 0 0 2734.687353 4258.980140 7772.212858 2 28 NaN 6.3750
13582 12568 3 NaN NaN NaN NaN 4 NaN NaN NaN ... 0 0 0 2734.687353 4258.980140 7772.212858 2 28 NaN NaN
13583 12569 1 NaN NaN NaN NaN 3 NaN NaN NaN ... 0 0 0 2580.967613 2925.167116 5075.164946 2 61 NaN NaN
13584 12569 2 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 2580.967613 2925.167116 5075.164946 2 61 NaN 6.3750
13585 12570 1 NaN NaN NaN NaN 3 NaN NaN NaN ... 0 0 0 5181.311509 6205.829154 11325.017623 2 40 NaN NaN
13586 12570 2 NaN NaN NaN NaN 3 NaN NaN NaN ... 0 0 0 5181.311509 6205.829154 11325.017623 2 40 NaN NaN
13587 12570 3 NaN NaN NaN NaN 3 NaN NaN NaN ... 0 0 0 5181.311509 6205.829154 11325.017623 2 40 NaN NaN
13588 12571 1 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 4670.540953 5795.692880 6269.200989 1 78 NaN 6.1875
13589 12571 2 NaN NaN NaN NaN 3 NaN NaN NaN ... 0 0 0 4670.540953 5795.692880 6269.200989 1 78 NaN NaN
13590 12571 3 NaN NaN NaN NaN 3 NaN NaN NaN ... 0 0 0 4670.540953 5795.692880 6269.200989 1 78 NaN NaN
13591 12571 4 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 4670.540953 5795.692880 6269.200989 1 78 NaN 7.5000
13592 12571 5 NaN NaN NaN NaN 6 NaN 1 NaN ... 0 0 0 4670.540953 5795.692880 6269.200989 1 78 NaN 7.5000

13593 rows × 244 columns

Print value counts for birthord and compare to results published in the codebook


In [2]:
df.birthord.value_counts().sort_index()


Out[2]:
1     4413
2     2874
3     1234
4      421
5      126
6       50
7       20
8        7
9        2
10       1
dtype: int64

Print value counts for prglngth and compare to results published in the codebook


In [8]:
# Ex1 "Print value counts for prglngth and compare to results published in the codebook"
# Data http://www.icpsr.umich.edu/nsfg6/Controller?displayPage=labelDetails&fileCode=PREG&section=A&subSec=8016&srtLabel=611931

def ex1pr1():
    prglngth = df.prglngth
    printf(prglngth.value_counts().sort_index())

    print(type(prglngth), "\n")

    print("13 WEEKS OR LESS: {0}"    .format(prglngth[prglngth <= 13].count()))
    print("14-26 WEEKS: {0}"         .format(prglngth[(13 < prglngth) & (prglngth <= 26)].count()))
    print("27 WEEKS OR LONGER: {0}"  .format(prglngth[prglngth > 26].count()))
    print("\nTOTAL: {0}"             .format(len(prglngth))) 


ex1pr1()


0       15
1        9
2       78
3      151
4      412
5      181
6      543
7      175
8      409
9      594
10     137
11     202
12     170
13     446
14      29
15      39
16      44
17     253
18      17
19      34
20      18
21      37
22     147
23      12
24      31
25      15
26     117
27       8
28      38
29      23
30     198
31      29
32     122
33      50
34      60
35     357
36     329
37     457
38     609
39    4744
40    1120
41     591
42     328
43     148
44      46
45      10
46       1
47       1
48       7
50       2
dtype: int64
<class 'pandas.core.series.Series'> 

13 WEEKS OR LESS: 3522
14-26 WEEKS: 793
27 WEEKS OR LONGER: 9278

TOTAL: 13593

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 [11]:
# Ex1 "Print value counts for agepreg and compare to results published in the codebook."
# Data http://www.icpsr.umich.edu/nsfg6/Controller?displayPage=labelDetails&fileCode=PREG&section=A&subSec=8016&srtLabel=611935

def ex1pr2():
    agepreg = df.agepreg
    printf(agepreg.value_counts().sort_index())

    print(type(agepreg), "\n")

    print("INAPPLICABLE: {0}"  .format(agepreg.isnull().sum()))
    print("UNDER 20 YEARS: {0}".format(df[df.agepreg < 20].agepreg.count()))
    print("20-24 YEARS: {0}"   .format(len(agepreg[(20 <= agepreg) & (agepreg < 25)])))  #条件が複数ある時は()でくくる。演算子はビット演算子を使う。
    print("25-29 YEARS: {0}"   .format(len(df.query("agepreg >= 25 & agepreg < 30"))))   #queryも利用できる
    print("30-44 YEARS: {0}"   .format(len([x for x in agepreg if x >= 30])))            #普通のリストと同様にリスト内包表記も使える

    print("\nTOTAL: {0}"  .format(len(agepreg)))  #len()は全要素数を、count()はNaNやNullを除いた要素数を返す
    print("TOTAL - INAPPLICABLE: {0}"  .format(agepreg.count()))


ex1pr2()


10.33      1
10.50      1
10.83      1
10.91      1
11.08      1
11.41      1
11.50      1
11.75      2
12.08      1
12.25      1
12.41      2
12.50      1
12.66      2
12.75      1
12.83      2
12.91      1
13.00      2
13.08      1
13.16      1
13.25      4
13.33      1
13.50      3
13.58      2
13.66      5
13.75      4
13.83      5
13.91      5
14.00      5
14.08      7
14.16     12
14.25      8
14.33      4
14.41      3
14.50     11
14.58     10
14.66      7
14.75     11
14.83     14
14.91     14
15.00     14
15.08     11
15.16     12
15.25     12
15.33     23
15.41     17
15.50     28
15.58     28
15.66     27
15.75     29
15.83     25
15.91     28
16.00     31
16.08     42
16.16     32
16.25     32
16.33     40
16.41     22
16.50     38
16.58     28
16.66     30
16.75     43
16.83     36
16.91     40
17.00     55
17.08     46
17.16     38
17.25     47
17.33     54
17.41     65
17.50     48
17.58     59
17.66     47
17.75     58
17.83     62
17.91     61
18.00     67
18.08     58
18.16     58
18.25     71
18.33     66
18.41     79
18.50     59
18.58     80
18.66     71
18.75     73
18.83     90
18.91     85
19.00     67
19.08     69
19.16     70
19.25     65
19.33     78
19.41     61
19.50     81
19.58     76
19.66     78
19.75     72
19.83     75
19.91     66
20.00     91
20.08     80
20.16     66
20.25     75
20.33     72
20.41     82
20.50     82
20.58     71
20.66     69
20.75     76
20.83     80
20.91     83
21.00     76
21.08     66
21.16     58
21.25     90
21.33     77
21.41     81
21.50     66
21.58     83
21.66     75
21.75     92
21.83     87
21.91     72
22.00     59
22.08     64
22.16     57
22.25     83
22.33     73
22.41     56
22.50     62
22.58     53
22.66     60
22.75    100
22.83     70
22.91     55
23.00     67
23.08     81
23.16     76
23.25     78
23.33     65
23.41     69
23.50     58
23.58     75
23.66     61
23.75     69
23.83     65
23.91     65
24.00     57
24.08     56
24.16     63
24.25     81
24.33     64
24.41     66
24.50     64
24.58     64
24.66     60
24.75     66
24.83     79
24.91     55
25.00     58
25.08     70
25.16     50
25.25     69
25.33     62
25.41     65
25.50     56
25.58     54
25.66     64
25.75     60
25.83     58
25.91     56
26.00     61
26.08     49
26.16     69
26.25     64
26.33     57
26.41     53
26.50     54
26.58     59
26.66     63
26.75     44
26.83     57
26.91     56
27.00     60
27.08     55
27.16     62
27.25     50
27.33     46
27.41     54
27.50     54
27.58     57
27.66     50
27.75     58
27.83     53
27.91     58
28.00     53
28.08     49
28.16     55
28.25     45
28.33     53
28.41     39
28.50     54
28.58     46
28.66     47
28.75     54
28.83     44
28.91     50
29.00     43
29.08     44
29.16     39
29.25     54
29.33     40
29.41     48
29.50     34
29.58     29
29.66     44
29.75     53
29.83     47
29.91     49
30.00     48
30.08     54
30.16     41
30.25     42
30.33     42
30.41     38
30.50     41
30.58     55
30.66     36
30.75     37
30.83     53
30.91     45
31.00     37
31.08     36
31.16     34
31.25     41
31.33     36
31.41     37
31.50     38
31.58     40
31.66     34
31.75     40
31.83     23
31.91     36
32.00     38
32.08     20
32.16     25
32.25     30
32.33     35
32.41     38
32.50     40
32.58     21
32.66     45
32.75     26
32.83     32
32.91     27
33.00     24
33.08     38
33.16     34
33.25     23
33.33     27
33.41     29
33.50     18
33.58     21
33.66     22
33.75     19
33.83     27
33.91     29
34.00     25
34.08     25
34.16     23
34.25     19
34.33     25
34.41     23
34.50     20
34.58     20
34.66     26
34.75     17
34.83     11
34.91     17
35.00     14
35.08     21
35.16     18
35.25     26
35.33     12
35.41     22
35.50     18
35.58     15
35.66     14
35.75     17
35.83      7
35.91     19
36.00     22
36.08      7
36.16      9
36.25     16
36.33     20
36.41     14
36.50      6
36.58      9
36.66     18
36.75     13
36.83      7
36.91      6
37.00     12
37.08     13
37.16     13
37.25     13
37.33     16
37.41     12
37.50     11
37.58     13
37.66      7
37.75      5
37.83     11
37.91      4
38.00     16
38.08      8
38.16      9
38.25     11
38.33      6
38.41     10
38.50      7
38.58      4
38.66      8
38.75      6
38.83      3
38.91     11
39.00      7
39.08      6
39.16      5
39.25      6
39.33      1
39.41      2
39.50      7
39.58      5
39.66      2
39.75      4
39.83     10
39.91      5
40.00      5
40.08      2
40.25      6
40.33      4
40.50      6
40.58      4
40.66      4
40.75      2
40.83      4
40.91      4
41.00      2
41.08      2
41.16      2
41.25      6
41.33      1
41.41      2
41.58      4
41.75      3
41.83      1
41.91      4
42.08      2
42.16      1
42.25      1
42.33      1
42.41      1
42.50      2
42.58      2
42.75      2
43.00      3
43.16      1
43.25      4
43.58      3
43.91      1
44.08      1
Length: 373, dtype: int64
<class 'pandas.core.series.Series'> 

INAPPLICABLE: 352
UNDER 20 YEARS: 3182
20-24 YEARS: 4246
25-29 YEARS: 3178
30-44 YEARS: 2635

TOTAL: 13593
TOTAL - INAPPLICABLE: 13241

Compute the mean birthweight.


In [6]:
df.totalwgt_lb.mean()


Out[6]:
7.2656284576233681

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 [17]:
# Ex1 "Create a new column named totalwgt_kg that contains birth weight in kilograms. Compute its mean."

def ex1pr3():
    df['totalwgt_kg'] = df.totalwgt_lb / 2.2
    print(df[['totalwgt_kg', 'totalwgt_lb']])
    print("Mean(kg): {0}\nMean(pounds): {1}".format(df.totalwgt_kg.mean(), df.totalwgt_lb.mean()))


ex1pr3()


       totalwgt_kg  totalwgt_lb
0         4.005682       8.8125
1         3.579545       7.8750
2         4.147727       9.1250
3         3.181818       7.0000
4         2.812500       6.1875
5         3.892045       8.5625
6         4.346591       9.5625
7         3.806818       8.3750
8         3.437500       7.5625
9         3.011364       6.6250
10        3.551136       7.8125
11        3.181818       7.0000
12        1.818182       4.0000
13             NaN          NaN
14             NaN          NaN
15        3.494318       7.6875
16        3.409091       7.5000
17        2.869318       6.3125
18             NaN          NaN
19        3.977273       8.7500
20        3.721591       8.1875
21        2.528409       5.5625
22             NaN          NaN
23        3.068182       6.7500
24        3.352273       7.3750
25        3.096591       6.8125
26        3.693182       8.1250
27        3.238636       7.1250
28        2.755682       6.0625
29        3.380682       7.4375
...            ...          ...
13563     3.494318       7.6875
13564     3.465909       7.6250
13565     3.693182       8.1250
13566     3.409091       7.5000
13567          NaN          NaN
13568          NaN          NaN
13569     2.642045       5.8125
13570     3.039773       6.6875
13571     2.727273       6.0000
13572     2.642045       5.8125
13573     2.982955       6.5625
13574     2.784091       6.1250
13575          NaN          NaN
13576     2.926136       6.4375
13577          NaN          NaN
13578     2.727273       6.0000
13579     3.181818       7.0000
13580          NaN          NaN
13581     2.897727       6.3750
13582          NaN          NaN
13583          NaN          NaN
13584     2.897727       6.3750
13585          NaN          NaN
13586          NaN          NaN
13587          NaN          NaN
13588     2.812500       6.1875
13589          NaN          NaN
13590          NaN          NaN
13591     3.409091       7.5000
13592     3.409091       7.5000

[13593 rows x 2 columns]
Mean(kg): 3.302558389828803
Mean(pounds): 7.265628457623368

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 [18]:
# Ex1 "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."
# Explanation of variables: http://www.icpsr.umich.edu/nsfg6/Controller?displayPage=pregResp
# data(BABYSEX): http://www.icpsr.umich.edu/nsfg6/Controller?displayPage=labelDetails&fileCode=PREG&section=&subSec=8014&srtLabel=611801
def ex1pr4(df):
    #show_info_df(df)
    babysex = df['babysex']
    print(babysex.describe(), "\n")
    print(babysex.replace({1: "Male", 2:"Female"}))
    print("\nTotal: {0}\nMale: {1}\nFemale: {2}\nNaN: {3}\n".format(len(babysex), len(df[df.babysex == 1]), len(df[df.babysex == 2]), df.babysex.isnull().sum()))
    print("Percentage of Male: {0:.3f}%\nPercentage of Feale: {1:.3f}%".format((len(df[df.babysex == 1]) * 100 / df.babysex.count()), (len(df[df.babysex == 2]) * 100 / df.babysex.count())))


ex1pr4(df)


count    9141.000000
mean        1.492287
std         0.499968
min         1.000000
25%         1.000000
50%         1.000000
75%         2.000000
max         2.000000
dtype: float64 

0       Male
1     Female
2       Male
3     Female
4     Female
5       Male
6     Female
7     Female
8       Male
9     Female
10      Male
11    Female
12      Male
13       NaN
14       NaN
...
13578    Female
13579      Male
13580       NaN
13581      Male
13582       NaN
13583       NaN
13584    Female
13585       NaN
13586       NaN
13587       NaN
13588      Male
13589       NaN
13590       NaN
13591      Male
13592      Male
Name: babysex, Length: 13593, dtype: object

Total: 13593
Male: 4641
Female: 4500
NaN: 4452

Percentage of Male: 50.771%
Percentage of Feale: 49.229%

Create a boolean Series.


In [4]:
df.outcome == 1


Out[4]:
0      True
1      True
2      True
3      True
4      True
5      True
6      True
7      True
8      True
9      True
10     True
11     True
12     True
13    False
14    False
...
13578     True
13579     True
13580    False
13581     True
13582    False
13583    False
13584     True
13585    False
13586    False
13587    False
13588     True
13589    False
13590    False
13591     True
13592     True
Name: outcome, Length: 13593, dtype: bool

Use a boolean Series to select the records for the pregnancies that ended in live birth.


In [5]:
live = df[df.outcome == 1]
len(live)


Out[5]:
9148

Count the number of live births with birthwgt_lb between 0 and 5 pounds (including both). The result should be 1125.


In [6]:
len(live[(live.birthwgt_lb >= 0) & (live.birthwgt_lb <= 5)])


Out[6]:
1125

Count the number of live births with birthwgt_lb between 9 and 95 pounds (including both). The result should be 798


In [19]:
# Ex1 "Count the number of live births with birthwgt_lb between 9 and 95 pounds (including both). The result should be 798"

def ex1pr5(df):
    live = df[df.outcome == 1]
    birthwgt_lb = df.birthwgt_lb

    print("From 9 to 95 pounds: {0}".format(len(live.query("birthwgt_lb >= 9 & birthwgt_lb <= 95"))))


ex1pr5(df)


From 9 to 95 pounds: 798

Use birthord to select the records for first babies and others. How many are there of each?


In [7]:
firsts = df[df.birthord==1]
others = df[df.birthord>1]
len(firsts), len(others)


Out[7]:
(4413, 4735)

Compute the mean weight for first babies and others.


In [8]:
firsts.totalwgt_lb.mean()


Out[8]:
7.201094430437772

In [9]:
others.totalwgt_lb.mean()


Out[9]:
7.3258556149732623

Compute the mean prglngth for first babies and others. Compute the difference in means, expressed in hours.


In [20]:
# Ex1 "Compute the mean prglngth for first babies and others. Compute the difference in means, expressed in hours."

def ex1pr6(df):
    firsts = df[df.birthord == 1]
    others = df[df.birthord > 1]
    weeks_f = firsts['prglngth'].mean()
    weeks_o = others['prglngth'].mean()
    difference = (weeks_f - weeks_o) * 7 * 24

    print("1st babies prglngth(mean): {0:.3} WEEKS".format(weeks_f))
    print("Other babies prglngth(mean): {0:.3} WEEKS".format(weeks_o))
    print("Difference(firsts - others): {0:.3} hours".format(difference))

ex1pr6(df)


1st babies prglngth(mean): 38.6 WEEKS
Other babies prglngth(mean): 38.5 WEEKS
Difference(firsts - others): 13.1 hours

In [ ]: