<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Sparkling Titanic
Step1:  Here, I want to find the percentance of people that survived based on their class
pclass
Step2:  As expected, most of the upper class survived, while the lower class had less odds to survive.
Now, I am interested on the average price per ticket for each class.
<ASSISTANT_TASK:>
Python Code:
# imports
from pyspark import SparkContext
from pyspark.sql import SQLContext
from pyspark.sql.types import *
from pyspark.sql import functions as F
from graphframes import *
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
# make graphs beautiful
plt.style.use('ggplot')
plt.rc('font', family='sans-serif') 
%matplotlib inline
sqlContext = SQLContext(sc)
    
train = pd.read_csv('train.csv')
train.head()
sorted_by_age = train.sort_values(['Age'])
plt.plot(sorted_by_age.Age, sorted_by_age.Pclass, 'bo')
plt.title('Class per Age')
plt.ylim([0, 5])
plt.show()
# drop useless columns
drop_elements = ['PassengerId', 'Name']
train = train.drop(drop_elements, axis = 1)
train.head()
Explanation: Sparkling Titanic
End of explanation
total_elements = train.groupby(train.Pclass).Survived.count()
total_survived = train[train.Survived == 1].groupby(train.Pclass).Survived.count()
(total_survived / total_elements).plot.bar()
Explanation: Here, I want to find the percentance of people that survived based on their class
pclass: A proxy for socio-economic status (SES)  
1st = Upper
2nd = Middle
3rd = Lower
End of explanation
avg_prices = train.groupby(train.Pclass).Fare.mean()
avg_prices.plot.bar()
from pyspark.sql import SQLContext
sqlContext = SQLContext(sc)
df = spark.read.csv('train.csv', header='true', inferSchema='true')
df.limit(5).toPandas()
df.dtypes
df.filter((df.Survived == 1) & (df.Sex == 'male')).count() / df.filter(df.Sex == 'male').count()
df.filter((df.Survived == 1) & (df.Sex == 'female')).count() / df.filter(df.Sex == 'female').count()
total_by_sex = df.groupBy(df.Sex).count().withColumnRenamed('count', 'total')
survived_by_sex = df.filter(df.Survived == 1).groupBy(df.Sex).count()
survived_by_sex.join(total_by_sex, survived_by_sex.Sex == total_by_sex.Sex, 'outer')\
    .rdd.map(lambda x: (x.Sex, x['count'] / x.total)).collect()
# best way to do this calculation
df[["Sex", "Survived"]].groupby('Sex').mean().show()
g = sns.FacetGrid(train, col='Survived')
g.map(plt.hist, 'Age', bins=40)
grid = sns.FacetGrid(train, col='Pclass', row='Survived')
grid.map(plt.hist, 'Age', bins=40)
grid.add_legend();
Explanation: As expected, most of the upper class survived, while the lower class had less odds to survive.
Now, I am interested on the average price per ticket for each class.
End of explanation
<END_TASK>