Skip to content

Commit

Permalink
Adding groupby example
Browse files Browse the repository at this point in the history
  • Loading branch information
AllenDowney committed Feb 13, 2019
1 parent 1c208dc commit 9424efc
Showing 1 changed file with 46 additions and 97 deletions.
143 changes: 46 additions & 97 deletions examples/groupby_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 162 ms, sys: 44 ms, total: 206 ms\n",
"Wall time: 205 ms\n"
"CPU times: user 164 ms, sys: 40.2 ms, total: 204 ms\n",
"Wall time: 203 ms\n"
]
},
{
Expand Down Expand Up @@ -467,6 +467,8 @@
"\n",
"Now we can see how that fraction depends on age, cohort (year of birth), and period (year of interview).\n",
"\n",
"### Group by year\n",
"\n",
"First we'll group respondents by year."
]
},
Expand All @@ -478,7 +480,7 @@
{
"data": {
"text/plain": [
"<pandas.core.groupby.groupby.DataFrameGroupBy object at 0x7fbb170ab240>"
"<pandas.core.groupby.groupby.DataFrameGroupBy object at 0x7f49bdd8ac18>"
]
},
"execution_count": 10,
Expand Down Expand Up @@ -618,7 +620,7 @@
{
"data": {
"text/plain": [
"<pandas.core.groupby.groupby.SeriesGroupBy object at 0x7fbb170a8c50>"
"<pandas.core.groupby.groupby.SeriesGroupBy object at 0x7f49bdd874e0>"
]
},
"execution_count": 13,
Expand All @@ -639,85 +641,44 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"18.0 0.29577464788732394\n",
"19.0 0.41486486486486485\n",
"20.0 0.42333333333333334\n",
"21.0 0.4105409153952843\n",
"22.0 0.44170096021947874\n",
"23.0 0.3832923832923833\n",
"24.0 0.43866666666666665\n",
"25.0 0.3870967741935484\n",
"26.0 0.40343915343915343\n",
"27.0 0.39238845144356954\n",
"28.0 0.38896189224704336\n",
"29.0 0.3788265306122449\n",
"30.0 0.37897310513447435\n",
"31.0 0.36792452830188677\n",
"32.0 0.3354192740926158\n",
"33.0 0.3146341463414634\n",
"34.0 0.33994708994708994\n",
"35.0 0.306803594351733\n",
"36.0 0.3380829015544041\n",
"37.0 0.35704514363885087\n",
"38.0 0.3\n",
"39.0 0.2785388127853881\n",
"40.0 0.2808842652795839\n",
"41.0 0.282336578581363\n",
"42.0 0.28550074738415543\n",
"43.0 0.2695139911634757\n",
"44.0 0.24571428571428572\n",
"45.0 0.28794992175273865\n",
"46.0 0.27204783258594917\n",
"47.0 0.32192846034214617\n",
"48.0 0.2355848434925865\n",
"49.0 0.27044025157232704\n",
"50.0 0.2618629173989455\n",
"51.0 0.2739273927392739\n",
"52.0 0.2996688741721854\n",
"53.0 0.3322475570032573\n",
"54.0 0.27979274611398963\n",
"55.0 0.23416506717850288\n",
"56.0 0.30139372822299654\n",
"57.0 0.2423580786026201\n",
"58.0 0.2976827094474153\n",
"59.0 0.28256513026052105\n",
"60.0 0.24521072796934865\n",
"61.0 0.23883928571428573\n",
"62.0 0.30451127819548873\n",
"63.0 0.2631578947368421\n",
"64.0 0.23842592592592593\n",
"65.0 0.24228028503562946\n",
"66.0 0.19338422391857507\n",
"67.0 0.2864450127877238\n",
"68.0 0.18235294117647058\n",
"69.0 0.1935483870967742\n",
"70.0 0.1918158567774936\n",
"71.0 0.2214765100671141\n",
"72.0 0.18670886075949367\n",
"73.0 0.16040955631399317\n",
"74.0 0.22302158273381295\n",
"75.0 0.1650485436893204\n",
"76.0 0.19491525423728814\n",
"77.0 0.13366336633663367\n",
"78.0 0.1625\n",
"79.0 0.17834394904458598\n",
"80.0 0.19230769230769232\n",
"81.0 0.2\n",
"82.0 0.09655172413793103\n",
"83.0 0.18584070796460178\n",
"84.0 0.14432989690721648\n",
"85.0 0.10227272727272728\n",
"86.0 0.11594202898550725\n",
"87.0 0.125\n",
"88.0 0.12962962962962962\n",
"89.0 0.1652892561983471\n"
"1972 nan\n",
"1973 0.20136518771331058\n",
"1974 nan\n",
"1975 0.22569198012775019\n",
"1976 0.29395604395604397\n",
"1977 nan\n",
"1978 0.3056501021102791\n",
"1980 0.2585844428871759\n",
"1982 nan\n",
"1983 0.23943661971830985\n",
"1984 0.2147887323943662\n",
"1985 nan\n",
"1986 0.17466945024356298\n",
"1987 0.15506508205998867\n",
"1988 0.1705170517051705\n",
"1989 0.17492416582406473\n",
"1990 0.15940366972477063\n",
"1991 0.17775467775467776\n",
"1993 0.24342745861733203\n",
"1994 0.24722075172048702\n",
"1996 0.28713910761154854\n",
"1998 0.2966589861751152\n",
"2000 0.3395810363836825\n",
"2002 0.3403755868544601\n",
"2004 0.35785536159601\n",
"2006 0.3435155412647374\n",
"2008 0.4001597444089457\n",
"2010 0.4773988897700238\n",
"2012 0.4835341365461847\n",
"2014 0.5790147152911068\n",
"2016 0.5911039657020365\n"
]
}
],
Expand All @@ -735,7 +696,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 15,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -776,7 +737,7 @@
"Name: grass, dtype: float64"
]
},
"execution_count": 14,
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -795,7 +756,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 16,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -829,7 +790,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 17,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -899,7 +860,7 @@
"Name: cohort, Length: 116, dtype: int64"
]
},
"execution_count": 16,
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -917,7 +878,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 18,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -960,7 +921,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 19,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -991,20 +952,8 @@
"source": [
"Younger people are more likely to support legalization than old people.\n",
"\n",
"In general, it is not easy to separate age, "
"In general, it is not easy to separate period, cohort, and age effects, but there are ways. We'll come back to this example to see how."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit 9424efc

Please sign in to comment.