-
Notifications
You must be signed in to change notification settings - Fork 8
array_to_dict_reducer
Hassan Syyid edited this page Feb 11, 2021
·
2 revisions
Combines array of JSON dictionaries to a single dictionary with an aggregated value.
array_to_dict_reducer(key_prop, value_prop):
"""
:param key_prop: property in dictionary for key
:param value_prop: property in dictionary for value
:return: a dictionary that has all the accumulated values
"""
Suppose we had the following dataset.
Observe column CustomField
with serialized JSON data in the following format:
[{"DefinitionId": "1", "Name": "Crew #", "Type": "StringType", "StringValue": "102"}]
To explode this, we'll need to reduce this as we only care about the Name
and StringValue
properties. We can use the explode_json_to_cols
function with an array_to_dict_reducer
to accomplish this.
# Grab the string value of entries
invoices = invoices.pipe(gs.explode_json_to_cols, 'CustomField', reducer=gs.array_to_dict_reducer('Name', 'StringValue'))
invoices[['Id', 'CustomField.Crew #']].head()