4. Extending Python, Files, Errors, and Graphs
Activity 13: Visualizing the Titanic Dataset Using a Pie Chart and Bar Plots
Solution
- Import all the lines from the
csvfile in thetitanic_train.csvdataset file and store it in a list:import csv lines = [] with open('titanic_train.csv') as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') for line in csv_reader: lines.append(line) - Generate a collection of
passengersobjects. This step is designed to facilitate the subsequent steps where we need to extract values of different properties into a list for generating charts:data = lines[1:] passengers = [] headers = lines[0]
- Create a simple
forloop for thedvariable indata, which will store the values in a list:for d in data: p = {} for i in range(0,len(headers)): &...