<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Groupby operations
Some imports
Step1:  Some 'theory'
Step2:  Using the filtering and reductions operations we have seen in the previous notebooks, we could do something like
Step3:  And many more methods are available. 
And now applying this on some real data
We go back to the titanic survival data
Step4:  <div class="alert alert-success">
    <b>EXERCISE</b>
Step5:  <div class="alert alert-success">
    <b>EXERCISE</b>
Step6:  <div class="alert alert-success">
    <b>EXERCISE</b>
Step7:  <div class="alert alert-success">
    <b>EXERCISE</b>
Step8:  <div class="alert alert-success">
    <b>EXERCISE</b>
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
try:
    import seaborn
except ImportError:
    pass
pd.options.display.max_rows = 10
Explanation: Groupby operations
Some imports:
End of explanation
df = pd.DataFrame({'key':['A','B','C','A','B','C','A','B','C'],
                   'data': [0, 5, 10, 5, 10, 15, 10, 15, 20]})
df
Explanation: Some 'theory': the groupby operation (split-apply-combine)
The "group by" concept: we want to apply the same function on subsets of your dataframe, based on some key to split the dataframe in subsets
This operation is also referred to as the "split-apply-combine" operation, involving the following steps:
Splitting the data into groups based on some criteria
Applying a function to each group independently
Combining the results into a data structure
<img src="img/splitApplyCombine.png">
Similar to SQL GROUP BY
The example of the image in pandas syntax:
End of explanation
df.groupby('key').aggregate(np.sum)  # 'sum'
df.groupby('key').sum()
Explanation: Using the filtering and reductions operations we have seen in the previous notebooks, we could do something like:
df[df['key'] == "A"].sum()
df[df['key'] == "B"].sum()
...
But pandas provides the groupby method to do this:
End of explanation
df = pd.read_csv("data/titanic.csv")
df.head()
Explanation: And many more methods are available. 
And now applying this on some real data
We go back to the titanic survival data:
End of explanation
df.groupby('Sex')['Age'].mean()
Explanation: <div class="alert alert-success">
    <b>EXERCISE</b>: Using groupby(), calculate the average age for each sex.
</div>
End of explanation
df['Survived'].sum() / len(df['Survived'])
Explanation: <div class="alert alert-success">
    <b>EXERCISE</b>: Calculate the average survival ratio for all passengers.
</div>
End of explanation
df25 = df[df['Age'] <= 25]
df25['Survived'].sum() / len(df25['Survived'])
Explanation: <div class="alert alert-success">
    <b>EXERCISE</b>: Calculate this survival ratio for all passengers younger that 25 (remember: filtering/boolean indexing).
</div>
End of explanation
def survival_ratio(survived):
    return survived.sum() / len(survived)
df.groupby('Sex')['Survived'].aggregate(survival_ratio)
Explanation: <div class="alert alert-success">
    <b>EXERCISE</b>: Is there a difference in this survival ratio between the sexes? (tip: write the above calculation of the survival ratio as a function)
</div>
End of explanation
df.groupby('Pclass')['Survived'].aggregate(survival_ratio).plot(kind='bar')
Explanation: <div class="alert alert-success">
    <b>EXERCISE</b>: Make a bar plot of the survival ratio for the different classes ('Pclass' column).
</div>
End of explanation
<END_TASK>