import pandas as pds
datas = pds.read_csv('titanic.csv')
datas.head()
| survived | pclass | sex | age | sibsp | parch | fare | embarked | class | who | adult_male | deck | embark_town | alive | alone | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 3 | male | 22.0 | 1 | 0 | 7.2500 | S | Third | man | True | NaN | Southampton | no | False |
| 1 | 1 | 1 | female | 38.0 | 1 | 0 | 71.2833 | C | First | woman | False | C | Cherbourg | yes | False |
| 2 | 1 | 3 | female | 26.0 | 0 | 0 | 7.9250 | S | Third | woman | False | NaN | Southampton | yes | True |
| 3 | 1 | 1 | female | 35.0 | 1 | 0 | 53.1000 | S | First | woman | False | C | Southampton | yes | False |
| 4 | 0 | 3 | male | 35.0 | 0 | 0 | 8.0500 | S | Third | man | True | NaN | Southampton | no | True |
res=datas[['pclass', 'survived', 'sex']].groupby(['sex', 'pclass']).mean()
print(res)
survived
sex pclass
female 1 0.968085
2 0.921053
3 0.500000
male 1 0.368852
2 0.157407
3 0.135447
import matplotlib.pyplot as plt
res.plot(kind='bar')
plt.show()
res=datas[['pclass', 'survived', 'sex']].pivot_table(index = 'pclass', columns = 'sex')
res
| survived | ||
|---|---|---|
| sex | female | male |
| pclass | ||
| 1 | 0.968085 | 0.368852 |
| 2 | 0.921053 | 0.157407 |
| 3 | 0.500000 | 0.135447 |
import matplotlib.pyplot as plt
res.plot(kind='bar')
plt.show()
import numpy as np
import pandas as pds
import seaborn as sns
datas = sns.load_dataset('titanic') # csv de gitub
datas.columns
datas.head()
res = datas.pivot_table('survived',
aggfunc=np.mean,
index='class',
columns='sex')
res
| sex | female | male |
|---|---|---|
| class | ||
| First | 0.968085 | 0.368852 |
| Second | 0.921053 | 0.157407 |
| Third | 0.500000 | 0.135447 |
import matplotlib.pyplot as plt
res.plot(kind='bar')
plt.show()