...

/

Solution: Task I to Task IV

Solution: Task I to Task IV

The solution to the previously assigned challenges: Task I to Task IV.

Brace yourself, it’s time to cover each task turn by turn to work out the project in a systematic manner. You can run each executable and verify the answers for the different values.

Explanation

Task 1: Group the data

To group the columns with their respective values, we need the columns’ names and the data. There was one more requirement. We need to decide on the number of records being grouped but we have to keep it optional. So, we also need a keyword argument: n. The header of the function looks like:

def group_values_with_columns(orig_data, req_col, **kwargs)

Here, orig_data is the list (data) in which the data from the csv file was read beforehand. Additionally, req_col is required_columns that we hardcoded before. Look at the following snippet.

Press + to interact
import csv
from collections import namedtuple
def group_values_with_columns(orig_data, req_col, **kwargs):
# Namedtuple to create a pair with column and its values
Student = namedtuple('Student', 'GPA Gender drink exercise fries income sports weight')
columns_index = [orig_data[0].index(name) for name in req_col]
if kwargs: # if limit is passed
individuals = orig_data[1:kwargs['n']+1]
else:
individuals = orig_data[1:51] # 50 by default
grouped_data = [] # List of tuples of columns and values
for individual in individuals:
value = [individual[i] for i in columns_index]
grouped_data.append(Student._make(value))
return grouped_data
# Reading the dataset in a list
with open('food.csv', newline='') as f:
reader = csv.reader(f)
data = list(reader) # contains the data in the raw format.
# Columns you'll deal with in this project
required_columns = ['GPA', 'Gender', 'drink', 'exercise', 'fries', 'income', 'sports', 'weight']
# Calling the function
grouped_data = group_values_with_columns(data, required_columns, n=60)
for entry in grouped_data:
print(entry)

The easiest way to create an association with values is to use a namedtuple. We create a Student tuple with the fields: GPA, Gender, drink, ...