Path: blob/master/03_Grouping/Regiment/Solutions.ipynb
613 views
Kernel: Python 2
Regiment
Introduction:
Special thanks to: http://chrisalbon.com/ for sharing the dataset and materials.
Step 1. Import the necessary libraries
In [1]:
Step 2. Create the DataFrame with the following values:
In [4]:
Step 3. Assign it to a variable called regiment.
Don't forget to name each column
In [6]:
Out[6]:
Step 4. What is the mean preTestScore from the regiment Nighthawks?
In [26]:
Out[26]:
Step 5. Present general statistics by company
In [29]:
Out[29]:
Step 6. What is the mean of each company's preTestScore?
In [33]:
Out[33]:
company
1st 6.666667
2nd 15.500000
Name: preTestScore, dtype: float64
Step 7. Present the mean preTestScores grouped by regiment and company
In [35]:
Out[35]:
regiment company
Dragoons 1st 3.5
2nd 27.5
Nighthawks 1st 14.0
2nd 16.5
Scouts 1st 2.5
2nd 2.5
Name: preTestScore, dtype: float64
Step 8. Present the mean preTestScores grouped by regiment and company without heirarchical indexing
In [36]:
Out[36]:
Step 9. Group the entire dataframe by regiment and company
In [37]:
Out[37]:
Step 10. What is the number of observations in each regiment and company
In [41]:
Out[41]:
company regiment
1st Dragoons 2
Nighthawks 2
Scouts 2
2nd Dragoons 2
Nighthawks 2
Scouts 2
dtype: int64
Step 11. Iterate over a group and print the name and the whole data from the regiment
In [50]:
Out[50]:
Dragoons
regiment company name preTestScore postTestScore
4 Dragoons 1st Cooze 3 70
5 Dragoons 1st Jacon 4 25
6 Dragoons 2nd Ryaner 24 94
7 Dragoons 2nd Sone 31 57
Nighthawks
regiment company name preTestScore postTestScore
0 Nighthawks 1st Miller 4 25
1 Nighthawks 1st Jacobson 24 94
2 Nighthawks 2nd Ali 31 57
3 Nighthawks 2nd Milner 2 62
Scouts
regiment company name preTestScore postTestScore
8 Scouts 1st Sloan 2 62
9 Scouts 1st Piger 3 70
10 Scouts 2nd Riani 2 62
11 Scouts 2nd Ali 3 70