# Run This to Import Libraries
import pandas as pd
import numpy as np
# Run this to collect Titanic Data
titanic = pd.read_csv('titanic_cleaned.csv')
titanic
sexes = list(titanic['Sex'])
def count(males_and_females):
males = 0
females = 0
for person in ...:
if ...:
males += 1
else:
females += 1
print('Number of males: ' + str(males))
print('Number of females: ' + str(females))
count(sexes)
sexes = np.array(titanic['Sex'])
def countSexes(array):
males = ...(...)
females = ...(...)
# Don't edit the code below this line
print('Number of males: ' + str(males))
print('Number of females: ' + str(females))
countSexes(sexes)
#Run this cell to get a list of classes and a list of fares on the Titanic.
classes = list(titanic['Class'])
fares = list(titanic['Fare'])
#solution code for average fare function
def average_fare_per_class(classes, fares):
total_fare_1 = 0
total_fare_2 = 0
total_fare_3 = 0
class1_count = 0
class2_count = 0
class3_count = 0
i = 0
while (i < ...):
if ...:
total_fare_1 += ...
class1_count += 1
elif ...:
total_fare_2 += ...
class2_count += 1
else:
total_fare_3 += ...
class3_count += 1
i += 1
return total_fare_1 / class1_count, total_fare_2 / class2_count, total_fare_3 / class3_count
print('Average fare of class 1: ' + str(average_fare_per_class(classes, fares)[0]))
print('Average fare of class 2: ' + str(average_fare_per_class(classes, fares)[1]))
print('Average fare of class 3: ' + str(average_fare_per_class(classes, fares)[2]))
# Run this cell to assign ages to an array corresponding to the Age column in the Titanic table
ages = np.array(titanic['Age'])
def averageAge(array):
num_of_passengers = ...(...)
sum_of_ages = ...(...)
return ... / ...
# Dont't edit the code below this line
averageAge(ages)
...(...)
# Run this cell to assign survivors to an array corresponding to the Survived column in the Titanic table
survivors = np.array(titanic['Survived'])
def countSurvivors(array):
return ...(...)
countSurvivors(survivors)
# Run this cell to assign group_sizes to an array corresponding to the Family Size column in the Titanic table
group_sizes = np.array(titanic['Family Size'])
def countSoloPassengers(array):
num_of_passengers = ...(...)
group_passengers = ...(...)
solo_passengers = ... - ...
# Don't edit the code below this line
return solo_passengers
countSoloPassengers(group_sizes)
ages = list(titanic['Age'])
survived = list(titanic['Survived'])
names = list(titanic['Name'])
def oldest_survivor(ages, survived, names):
oldest_survivor_age = 0
oldest_survivor_name = ''
i = 0
while (i < ...):
if ...:
if ... > ...:
oldest_survivor_age = ...
oldest_survivor_name = ...
i += 1
return oldest_survivor_name
oldest_survivor(ages, survived, names)