{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Regiment"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Introduction:\n",
"\n",
"Special thanks to: http://chrisalbon.com/ for sharing the dataset and materials.\n",
"\n",
"### Step 1. Import the necessary libraries"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 2. Create the DataFrame with the following values:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks', 'Dragoons', 'Dragoons', 'Dragoons', 'Dragoons', 'Scouts', 'Scouts', 'Scouts', 'Scouts'], \n",
" 'company': ['1st', '1st', '2nd', '2nd', '1st', '1st', '2nd', '2nd','1st', '1st', '2nd', '2nd'], \n",
" 'name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze', 'Jacon', 'Ryaner', 'Sone', 'Sloan', 'Piger', 'Riani', 'Ali'], \n",
" 'preTestScore': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 2, 3],\n",
" 'postTestScore': [25, 94, 57, 62, 70, 25, 94, 57, 62, 70, 62, 70]}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 3. Assign it to a variable called regiment.\n",
"#### Don't forget to name each column"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"
\n",
" \n",
" \n",
" | \n",
" regiment | \n",
" company | \n",
" name | \n",
" preTestScore | \n",
" postTestScore | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Nighthawks | \n",
" 1st | \n",
" Miller | \n",
" 4 | \n",
" 25 | \n",
"
\n",
" \n",
" 1 | \n",
" Nighthawks | \n",
" 1st | \n",
" Jacobson | \n",
" 24 | \n",
" 94 | \n",
"
\n",
" \n",
" 2 | \n",
" Nighthawks | \n",
" 2nd | \n",
" Ali | \n",
" 31 | \n",
" 57 | \n",
"
\n",
" \n",
" 3 | \n",
" Nighthawks | \n",
" 2nd | \n",
" Milner | \n",
" 2 | \n",
" 62 | \n",
"
\n",
" \n",
" 4 | \n",
" Dragoons | \n",
" 1st | \n",
" Cooze | \n",
" 3 | \n",
" 70 | \n",
"
\n",
" \n",
" 5 | \n",
" Dragoons | \n",
" 1st | \n",
" Jacon | \n",
" 4 | \n",
" 25 | \n",
"
\n",
" \n",
" 6 | \n",
" Dragoons | \n",
" 2nd | \n",
" Ryaner | \n",
" 24 | \n",
" 94 | \n",
"
\n",
" \n",
" 7 | \n",
" Dragoons | \n",
" 2nd | \n",
" Sone | \n",
" 31 | \n",
" 57 | \n",
"
\n",
" \n",
" 8 | \n",
" Scouts | \n",
" 1st | \n",
" Sloan | \n",
" 2 | \n",
" 62 | \n",
"
\n",
" \n",
" 9 | \n",
" Scouts | \n",
" 1st | \n",
" Piger | \n",
" 3 | \n",
" 70 | \n",
"
\n",
" \n",
" 10 | \n",
" Scouts | \n",
" 2nd | \n",
" Riani | \n",
" 2 | \n",
" 62 | \n",
"
\n",
" \n",
" 11 | \n",
" Scouts | \n",
" 2nd | \n",
" Ali | \n",
" 3 | \n",
" 70 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" regiment company name preTestScore postTestScore\n",
"0 Nighthawks 1st Miller 4 25\n",
"1 Nighthawks 1st Jacobson 24 94\n",
"2 Nighthawks 2nd Ali 31 57\n",
"3 Nighthawks 2nd Milner 2 62\n",
"4 Dragoons 1st Cooze 3 70\n",
"5 Dragoons 1st Jacon 4 25\n",
"6 Dragoons 2nd Ryaner 24 94\n",
"7 Dragoons 2nd Sone 31 57\n",
"8 Scouts 1st Sloan 2 62\n",
"9 Scouts 1st Piger 3 70\n",
"10 Scouts 2nd Riani 2 62\n",
"11 Scouts 2nd Ali 3 70"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 4. What is the mean preTestScore from the regiment Nighthawks? "
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" preTestScore | \n",
" postTestScore | \n",
"
\n",
" \n",
" regiment | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" Dragoons | \n",
" 15.50 | \n",
" 61.5 | \n",
"
\n",
" \n",
" Nighthawks | \n",
" 15.25 | \n",
" 59.5 | \n",
"
\n",
" \n",
" Scouts | \n",
" 2.50 | \n",
" 66.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" preTestScore postTestScore\n",
"regiment \n",
"Dragoons 15.50 61.5\n",
"Nighthawks 15.25 59.5\n",
"Scouts 2.50 66.0"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 5. Present general statistics by company"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" | \n",
" postTestScore | \n",
" preTestScore | \n",
"
\n",
" \n",
" company | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 1st | \n",
" count | \n",
" 6.000000 | \n",
" 6.000000 | \n",
"
\n",
" \n",
" mean | \n",
" 57.666667 | \n",
" 6.666667 | \n",
"
\n",
" \n",
" std | \n",
" 27.485754 | \n",
" 8.524475 | \n",
"
\n",
" \n",
" min | \n",
" 25.000000 | \n",
" 2.000000 | \n",
"
\n",
" \n",
" 25% | \n",
" 34.250000 | \n",
" 3.000000 | \n",
"
\n",
" \n",
" 50% | \n",
" 66.000000 | \n",
" 3.500000 | \n",
"
\n",
" \n",
" 75% | \n",
" 70.000000 | \n",
" 4.000000 | \n",
"
\n",
" \n",
" max | \n",
" 94.000000 | \n",
" 24.000000 | \n",
"
\n",
" \n",
" 2nd | \n",
" count | \n",
" 6.000000 | \n",
" 6.000000 | \n",
"
\n",
" \n",
" mean | \n",
" 67.000000 | \n",
" 15.500000 | \n",
"
\n",
" \n",
" std | \n",
" 14.057027 | \n",
" 14.652645 | \n",
"
\n",
" \n",
" min | \n",
" 57.000000 | \n",
" 2.000000 | \n",
"
\n",
" \n",
" 25% | \n",
" 58.250000 | \n",
" 2.250000 | \n",
"
\n",
" \n",
" 50% | \n",
" 62.000000 | \n",
" 13.500000 | \n",
"
\n",
" \n",
" 75% | \n",
" 68.000000 | \n",
" 29.250000 | \n",
"
\n",
" \n",
" max | \n",
" 94.000000 | \n",
" 31.000000 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" postTestScore preTestScore\n",
"company \n",
"1st count 6.000000 6.000000\n",
" mean 57.666667 6.666667\n",
" std 27.485754 8.524475\n",
" min 25.000000 2.000000\n",
" 25% 34.250000 3.000000\n",
" 50% 66.000000 3.500000\n",
" 75% 70.000000 4.000000\n",
" max 94.000000 24.000000\n",
"2nd count 6.000000 6.000000\n",
" mean 67.000000 15.500000\n",
" std 14.057027 14.652645\n",
" min 57.000000 2.000000\n",
" 25% 58.250000 2.250000\n",
" 50% 62.000000 13.500000\n",
" 75% 68.000000 29.250000\n",
" max 94.000000 31.000000"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 6. What is the mean each company's preTestScore?"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"company\n",
"1st 6.666667\n",
"2nd 15.500000\n",
"Name: preTestScore, dtype: float64"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 7. Present the mean preTestScores grouped by regiment and company"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"regiment company\n",
"Dragoons 1st 3.5\n",
" 2nd 27.5\n",
"Nighthawks 1st 14.0\n",
" 2nd 16.5\n",
"Scouts 1st 2.5\n",
" 2nd 2.5\n",
"Name: preTestScore, dtype: float64"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 8. Present the mean preTestScores grouped by regiment and company without heirarchical indexing"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" company | \n",
" 1st | \n",
" 2nd | \n",
"
\n",
" \n",
" regiment | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" Dragoons | \n",
" 3.5 | \n",
" 27.5 | \n",
"
\n",
" \n",
" Nighthawks | \n",
" 14.0 | \n",
" 16.5 | \n",
"
\n",
" \n",
" Scouts | \n",
" 2.5 | \n",
" 2.5 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
"company 1st 2nd\n",
"regiment \n",
"Dragoons 3.5 27.5\n",
"Nighthawks 14.0 16.5\n",
"Scouts 2.5 2.5"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 9. Group the entire dataframe by regiment and company"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" | \n",
" preTestScore | \n",
" postTestScore | \n",
"
\n",
" \n",
" regiment | \n",
" company | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" Dragoons | \n",
" 1st | \n",
" 3.5 | \n",
" 47.5 | \n",
"
\n",
" \n",
" 2nd | \n",
" 27.5 | \n",
" 75.5 | \n",
"
\n",
" \n",
" Nighthawks | \n",
" 1st | \n",
" 14.0 | \n",
" 59.5 | \n",
"
\n",
" \n",
" 2nd | \n",
" 16.5 | \n",
" 59.5 | \n",
"
\n",
" \n",
" Scouts | \n",
" 1st | \n",
" 2.5 | \n",
" 66.0 | \n",
"
\n",
" \n",
" 2nd | \n",
" 2.5 | \n",
" 66.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" preTestScore postTestScore\n",
"regiment company \n",
"Dragoons 1st 3.5 47.5\n",
" 2nd 27.5 75.5\n",
"Nighthawks 1st 14.0 59.5\n",
" 2nd 16.5 59.5\n",
"Scouts 1st 2.5 66.0\n",
" 2nd 2.5 66.0"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 10. What is the number of observations in each regiment and company"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"company regiment \n",
"1st Dragoons 2\n",
" Nighthawks 2\n",
" Scouts 2\n",
"2nd Dragoons 2\n",
" Nighthawks 2\n",
" Scouts 2\n",
"dtype: int64"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 11. Iterate over a group and print the name and the whole data from the regiment"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dragoons\n",
" regiment company name preTestScore postTestScore\n",
"4 Dragoons 1st Cooze 3 70\n",
"5 Dragoons 1st Jacon 4 25\n",
"6 Dragoons 2nd Ryaner 24 94\n",
"7 Dragoons 2nd Sone 31 57\n",
"Nighthawks\n",
" regiment company name preTestScore postTestScore\n",
"0 Nighthawks 1st Miller 4 25\n",
"1 Nighthawks 1st Jacobson 24 94\n",
"2 Nighthawks 2nd Ali 31 57\n",
"3 Nighthawks 2nd Milner 2 62\n",
"Scouts\n",
" regiment company name preTestScore postTestScore\n",
"8 Scouts 1st Sloan 2 62\n",
"9 Scouts 1st Piger 3 70\n",
"10 Scouts 2nd Riani 2 62\n",
"11 Scouts 2nd Ali 3 70\n"
]
}
],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}