<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Title
Step1:  "This grouped variable is now a GroupBy object. It has not actually computed anything yet except for some intermediate data about the group key df['key1']. The idea is that this object has all of the information needed to then apply some operation to each of the groups." - Python for Data Analysis
View a grouping
Use list() to show what a grouping looks like
Step2:  Descriptive statistics by group
Step3:  Mean of each regiment's preTestScore
Step4:  Mean preTestScores grouped by regiment and company
Step5:  Mean preTestScores grouped by regiment and company without heirarchical indexing
Step6:  Group the entire dataframe by regiment and company
Step7:  Number of observations in each regiment and company
Step8:  Iterate an operations over groups
Step9:  Group by columns
Specifically in this case
Step10:  In the dataframe "df", group by "regiments, take the mean values of the other variables for those groups, then display them with the prefix_mean
Step11:  Create a function to get the stats of a group
Step12:  Create bins and bin up postTestScore by those pins
Step13:  Apply the get_stats() function to each postTestScore bin
<ASSISTANT_TASK:>
Python Code:
# import modules
import pandas as pd
# Create dataframe
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]}
df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'name', 'preTestScore', 'postTestScore'])
df
# Create a groupby variable that groups preTestScores by regiment
groupby_regiment = df['preTestScore'].groupby(df['regiment'])
groupby_regiment
Explanation: Title: Apply Operations To Groups In Pandas
Slug: pandas_apply_operations_to_groups
Summary: Apply Operations To Groups In Pandas
Date: 2016-05-01 12:00
Category: Python
Tags: Data Wrangling
Authors: Chris Albon  
Preliminaries
End of explanation
list(df['preTestScore'].groupby(df['regiment']))
Explanation: "This grouped variable is now a GroupBy object. It has not actually computed anything yet except for some intermediate data about the group key df['key1']. The idea is that this object has all of the information needed to then apply some operation to each of the groups." - Python for Data Analysis
View a grouping
Use list() to show what a grouping looks like
End of explanation
df['preTestScore'].groupby(df['regiment']).describe()
Explanation: Descriptive statistics by group
End of explanation
groupby_regiment.mean()
Explanation: Mean of each regiment's preTestScore
End of explanation
df['preTestScore'].groupby([df['regiment'], df['company']]).mean()
Explanation: Mean preTestScores grouped by regiment and company
End of explanation
df['preTestScore'].groupby([df['regiment'], df['company']]).mean().unstack()
Explanation: Mean preTestScores grouped by regiment and company without heirarchical indexing
End of explanation
df.groupby(['regiment', 'company']).mean()
Explanation: Group the entire dataframe by regiment and company
End of explanation
df.groupby(['regiment', 'company']).size()
Explanation: Number of observations in each regiment and company
End of explanation
# Group the dataframe by regiment, and for each regiment,
for name, group in df.groupby('regiment'): 
    # print the name of the regiment
    print(name)
    # print the data of that regiment
    print(group)
Explanation: Iterate an operations over groups
End of explanation
list(df.groupby(df.dtypes, axis=1))
Explanation: Group by columns
Specifically in this case: group by the data types of the columns (i.e. axis=1) and then use list() to view what that grouping looks like
End of explanation
df.groupby('regiment').mean().add_prefix('mean_')
Explanation: In the dataframe "df", group by "regiments, take the mean values of the other variables for those groups, then display them with the prefix_mean
End of explanation
def get_stats(group):
    return {'min': group.min(), 'max': group.max(), 'count': group.count(), 'mean': group.mean()}
Explanation: Create a function to get the stats of a group
End of explanation
bins = [0, 25, 50, 75, 100]
group_names = ['Low', 'Okay', 'Good', 'Great']
df['categories'] = pd.cut(df['postTestScore'], bins, labels=group_names)
Explanation: Create bins and bin up postTestScore by those pins
End of explanation
df['postTestScore'].groupby(df['categories']).apply(get_stats).unstack()
Explanation: Apply the get_stats() function to each postTestScore bin
End of explanation
<END_TASK>