# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# # Regiment

# ### Introduction:
#
# Special thanks to: http://chrisalbon.com/ for sharing the dataset and materials.
#
# ### Step 1. Import the necessary libraries

# + jupyter={"outputs_hidden": false}
# sometimes sleep is more important... still, the I must make a git push to keep
# the habit going.
import pandas as pd
# -

# ### Step 2. Create the DataFrame with the following values:

# + tags=[]
raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks', 'Dragoons', 'Dragoons', 'Dragoons', 'Dragoons', 'Scouts', 'Scouts', 'Scouts', 'Scouts'], 
        'company': ['1st', '1st', '2nd', '2nd', '1st', '1st', '2nd', '2nd','1st', '1st', '2nd', '2nd'], 
        'name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze', 'Jacon', 'Ryaner', 'Sone', 'Sloan', 'Piger', 'Riani', 'Ali'], 
        'preTestScore': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 2, 3],
        'postTestScore': [25, 94, 57, 62, 70, 25, 94, 57, 62, 70, 62, 70]}
# -

# ### Step 3. Assign it to a variable called regiment.
# #### Don't forget to name each column

# + jupyter={"outputs_hidden": false}
regiment = pd.DataFrame(data=raw_data)
# -

regiment

# ### Step 4. What is the mean preTestScore from the regiment Nighthawks?  

# + jupyter={"outputs_hidden": false}
# Figured it out on my own!
regiment.groupby('regiment').preTestScore.mean()
# -

# ### Step 5. Present general statistics by company

# + jupyter={"outputs_hidden": false}
# Figured it out on my own!
regiment.groupby(['regiment', 'company']).describe()
# -

# ### Step 6. What is the mean of each company's preTestScore?

# + jupyter={"outputs_hidden": false}
# here is the given solution:
# regiment.groupby('company').preTestScore.mean()
# but I disagree because --> isn't the Dragoons 1st company different than the Scouts 1st?
regiment.groupby(['regiment', 'company']).preTestScore.mean()
# -

# ### Step 7. Present the mean preTestScores grouped by regiment and company

# + jupyter={"outputs_hidden": false}
regiment.groupby(['regiment', 'company']).preTestScore.mean()
# -

# ### Step 8. Present the mean preTestScores grouped by regiment and company without heirarchical indexing

# + jupyter={"outputs_hidden": false}
# Here is the given solution:
regiment.groupby(['regiment', 'company']).preTestScore.mean().unstack()

# but I still don't get this one entirely.
# -

# ### Step 9. Group the entire dataframe by regiment and company

# + jupyter={"outputs_hidden": false}
# not sure about this one yet...
regiment.groupby(['regiment', 'company'])

# given solution:
# regiment.groupby(['regiment', 'company']).mean()
# I could have done that, but it didn't say anything about finding the mean.
# I am still curious to know if you can present the whol dataframe again,
# but with the various columns grouped /*I bet you can.
# -

# ### Step 10. What is the number of observations in each regiment and company

# + jupyter={"outputs_hidden": false}
# I used .shape() at first, but that doesn't work.
regiment.groupby(['regiment', 'company']).size()
# -

# ### Step 11. Iterate over a group and print the name and the whole data from the regiment

# + jupyter={"outputs_hidden": false}
# I think I could have gotten this one, but may I still haven't had enough sleep yet.
# Here is the given solution:

# Group the dataframe by regiment, and for each regiment,
for name, group in regiment.groupby('regiment'):
    # print the name of the regiment
    print(name)
    # print the data of that regiment
    print(group)
# -



StarcoderdataJupyterScriptsDedupFiltered