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
.
- def extract_before_values(goat_records):
- '''
- Extract Before values in to a list.
- Parameters
- ----------
- goat_records : List-of-dictionaries.
- Data set.
- Returns
- -------
- befores : list
- List of before values.
- '''
- # Make a new list.
- befores = []
- # Loop over the record set
- for goat_record in goat_records:
- # Get Before value for the current record.
- before = goat_record['Before']
- # Add it to the list.
- befores.append(before)
- return befores
Where referenced