splitapplycombine
This commit is contained in:
parent
874f984bd5
commit
6085c2def3
1 changed files with 139 additions and 21 deletions
|
@ -10,7 +10,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 7,
|
||||||
"id": "8f9bc8b1",
|
"id": "8f9bc8b1",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
|
@ -31,7 +31,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 8,
|
||||||
"id": "d2dfebd3",
|
"id": "d2dfebd3",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
|
@ -41,7 +41,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 9,
|
||||||
"id": "09554c84",
|
"id": "09554c84",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
|
@ -51,7 +51,7 @@
|
||||||
"(6245, 18)"
|
"(6245, 18)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 3,
|
"execution_count": 9,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
|
@ -62,7 +62,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 10,
|
||||||
"id": "df95a10b",
|
"id": "df95a10b",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
|
@ -233,7 +233,7 @@
|
||||||
"4 No Yes No No 9 2.746064 No Control Madrid "
|
"4 No Yes No No 9 2.746064 No Control Madrid "
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 4,
|
"execution_count": 10,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
|
@ -257,7 +257,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 11,
|
||||||
"id": "99b21627-1b48-44ee-bda2-312b0718bd59",
|
"id": "99b21627-1b48-44ee-bda2-312b0718bd59",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
|
@ -275,12 +275,28 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 6,
|
"execution_count": 18,
|
||||||
"id": "00bb9eb1",
|
"id": "00bb9eb1",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"group\n",
|
||||||
|
"Control 96\n",
|
||||||
|
"MedDiet + Nuts 69\n",
|
||||||
|
"MedDiet + VOO 83\n",
|
||||||
|
"Name: event, dtype: int64"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 18,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"# your code here:\n"
|
"# your code here:\n",
|
||||||
|
"df.groupby('group')['event'].sum()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -293,12 +309,28 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 7,
|
"execution_count": 17,
|
||||||
"id": "db946d0f-8204-43a3-853c-41981a9811f4",
|
"id": "db946d0f-8204-43a3-853c-41981a9811f4",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"group\n",
|
||||||
|
"Control 2016\n",
|
||||||
|
"MedDiet + Nuts 2077\n",
|
||||||
|
"MedDiet + VOO 2152\n",
|
||||||
|
"Name: event, dtype: int64"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 17,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"# your code here:\n"
|
"# your code here:\n",
|
||||||
|
"df.groupby('group').count()['event']"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -313,12 +345,28 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 8,
|
"execution_count": 19,
|
||||||
"id": "13ad4130-2094-4e7a-a416-f0fd6e810413",
|
"id": "13ad4130-2094-4e7a-a416-f0fd6e810413",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"group\n",
|
||||||
|
"Control 4.761905\n",
|
||||||
|
"MedDiet + Nuts 3.322099\n",
|
||||||
|
"MedDiet + VOO 3.856877\n",
|
||||||
|
"Name: event, dtype: float64"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 19,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"# your code here:\n"
|
"# your code here:\n",
|
||||||
|
"df.groupby('group')['event'].mean()*100"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -352,12 +400,81 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 9,
|
"execution_count": 29,
|
||||||
"id": "5ab4e70e-6261-4a26-8ad9-14eae15be09c",
|
"id": "5ab4e70e-6261-4a26-8ad9-14eae15be09c",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/html": [
|
||||||
|
"<div>\n",
|
||||||
|
"<style scoped>\n",
|
||||||
|
" .dataframe tbody tr th:only-of-type {\n",
|
||||||
|
" vertical-align: middle;\n",
|
||||||
|
" }\n",
|
||||||
|
"\n",
|
||||||
|
" .dataframe tbody tr th {\n",
|
||||||
|
" vertical-align: top;\n",
|
||||||
|
" }\n",
|
||||||
|
"\n",
|
||||||
|
" .dataframe thead th {\n",
|
||||||
|
" text-align: right;\n",
|
||||||
|
" }\n",
|
||||||
|
"</style>\n",
|
||||||
|
"<table border=\"1\" class=\"dataframe\">\n",
|
||||||
|
" <thead>\n",
|
||||||
|
" <tr style=\"text-align: right;\">\n",
|
||||||
|
" <th>smoke</th>\n",
|
||||||
|
" <th>Current</th>\n",
|
||||||
|
" <th>Former</th>\n",
|
||||||
|
" <th>Never</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>group</th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>Control</th>\n",
|
||||||
|
" <td>4.92</td>\n",
|
||||||
|
" <td>8.04</td>\n",
|
||||||
|
" <td>3.47</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>MedDiet + Nuts</th>\n",
|
||||||
|
" <td>5.15</td>\n",
|
||||||
|
" <td>3.71</td>\n",
|
||||||
|
" <td>2.73</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>MedDiet + VOO</th>\n",
|
||||||
|
" <td>6.90</td>\n",
|
||||||
|
" <td>5.46</td>\n",
|
||||||
|
" <td>2.55</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </tbody>\n",
|
||||||
|
"</table>\n",
|
||||||
|
"</div>"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
"smoke Current Former Never\n",
|
||||||
|
"group \n",
|
||||||
|
"Control 4.92 8.04 3.47\n",
|
||||||
|
"MedDiet + Nuts 5.15 3.71 2.73\n",
|
||||||
|
"MedDiet + VOO 6.90 5.46 2.55"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 29,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"# your code here\n"
|
"# your code here\n",
|
||||||
|
"(df.pivot_table(index='group', columns='smoke', values='event')*100).round(2)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -392,7 +509,8 @@
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# your code here:\n"
|
"# your code here:\n",
|
||||||
|
"df.pivot_table(index='group', columns='smoke', values='age')"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
@ -412,7 +530,7 @@
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.11.5"
|
"version": "3.13.6"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue