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

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/samirlenka/pandas_exercises/blob/master/03_Grouping/Regiment/Exercises.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="zn3Ifb-jxk6I"
# # Regiment

# + [markdown] id="HLTR6zChxk6Q"
# ### Introduction:
#
# Special thanks to: http://chrisalbon.com/ for sharing the dataset and materials.
#
# ### Step 1. Import the necessary libraries

# + id="6yxP34Exxk6S"
import pandas as pd

# + [markdown] id="RpIIp4Bjxk6T"
# ### Step 2. Create the DataFrame with the following values:

# + id="o7INUdvUxk6T"
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]}

# + [markdown] id="BHQsHG4Uxk6T"
# ### Step 3. Assign it to a variable called regiment.
# #### Don't forget to name each column

# + id="50lyECRTxk6U" outputId="eeaef976-0112-4fcb-cc0c-0d7700b6c9bf" colab={"base_uri": "https://localhost:8080/", "height": 204}
regiment = pd.DataFrame(raw_data, columns = raw_data.keys())
regiment.head(5)

# + [markdown] id="XrcbPN5Exk6U"
# ### Step 4. What is the mean preTestScore from the regiment Nighthawks?  

# + id="YP-pwRqaxk6U" outputId="05651984-e6ff-464f-be9c-9f81cd328307" colab={"base_uri": "https://localhost:8080/", "height": 111}
regiment[regiment['regiment'] == 'Nighthawks'].groupby('regiment').mean()

# + [markdown] id="R3kLTtkrxk6V"
# ### Step 5. Present general statistics by company

# + id="wF9BHXRbxk6W" outputId="e1b62d18-4e90-4d1e-e1ec-db254581e574" colab={"base_uri": "https://localhost:8080/", "height": 173}
regiment.groupby('company').describe()

# + [markdown] id="a-GXdneyxk6X"
# ### Step 6. What is the mean of each company's preTestScore?

# + id="bMb_cQ1txk6X" outputId="76213038-7c3d-4ae9-b4d4-4553111c4245" colab={"base_uri": "https://localhost:8080/"}
regiment.groupby('company').preTestScore.mean()

# + [markdown] id="FPlmg6sixk6X"
# ### Step 7. Present the mean preTestScores grouped by regiment and company

# + id="zAeCgvEoxk6X" outputId="301aa18b-a2f0-48ee-fad4-db1fd051816c" colab={"base_uri": "https://localhost:8080/"}
regiment.groupby(['regiment', 'company']).preTestScore.mean()

# + [markdown] id="GwKyJTD2xk6Y"
# ### Step 8. Present the mean preTestScores grouped by regiment and company without heirarchical indexing

# + id="zfzv0Dcjxk6Y" outputId="517ab74f-39c3-400f-e8fc-9add34b46595" colab={"base_uri": "https://localhost:8080/", "height": 173}
regiment.groupby(['regiment','company']).preTestScore.mean().unstack()

# + [markdown] id="0pVFnAdSxk6Y"
# ### Step 9. Group the entire dataframe by regiment and company

# + id="7JesFN3Ixk6Y" outputId="7988387d-bd1a-4207-dae3-572d8030d5ca" colab={"base_uri": "https://localhost:8080/", "height": 266}
regiment.groupby(['regiment','company']).mean()

# + [markdown] id="8525iRf1xk6Z"
# ### Step 10. What is the number of observations in each regiment and company

# + id="3eWqWBKHxk6Z" outputId="ed335353-3455-4ad6-9c04-044cc7b57326" colab={"base_uri": "https://localhost:8080/"}
regiment.groupby(['company', 'regiment']).size()

# + [markdown] id="EgaD1JF5xk6Z"
# ### Step 11. Iterate over a group and print the name and the whole data from the regiment

# + id="pPCvB9t6xk6Z" outputId="21b2f6ef-cff8-49e8-b71a-251be5427749" colab={"base_uri": "https://localhost:8080/"}
for name,group in regiment.groupby('regiment'):
  print(name)
  print (group)





# 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