Data machine: Field extractor

Summary

Write a function that extracts a list with the values of one field from a data set.

Situation

You have a data set. You want to analyze one field using a function that takes a list as a param, rather than a data set.

Action

Say you have a data set called cleaned_goat_scores. Its records contain a field called Before.

Write a function you can call like this:

  • # Extract befores
  • befores = extract_before_values(cleaned_goat_scores)
  • # Analysis.
  • befores_mean = statistics.mean(befores)

statistics.mean wants a list as a param. extract_before_values creates the list, based on data in the list of dictionaries cleaned_goat_scores.

  1. def extract_before_values(goat_records):
  2.     '''
  3.     Extract Before values in to a list.
  4.  
  5.     Parameters
  6.     ----------
  7.     goat_records : List-of-dictionaries.
  8.         Data set.
  9.  
  10.     Returns
  11.     -------
  12.     befores : list
  13.         List of before values.
  14.  
  15.     '''
  16.     # Make a new list.
  17.     befores = []
  18.     # Loop over the record set
  19.     for goat_record in goat_records:
  20.         # Get Before value for the current record.
  21.         before = goat_record['Before']
  22.         # Add it to the list.
  23.         befores.append(before)
  24.     return befores
Where referenced