Say I have the following dataframe
id | dict_col
---+---------
1 {"age":[1,2],"name":["john","doe"]}
2 {"age":[3,4],"name":["foo","bar"]}
and the following
def unnest_dict(row):
age = row["age"]
name = row["name"]
return age,name
age,name = df["dict_col"].apply(unnest_dict)
that throws a ValueError:
ValueError: too many values to unpack (expected 2)
I can wrap the return into a tuple (age,name)
but then I need to loop over each of them afterwards like so
def unnest_dict(row):
.
.
return (age,name)
data = df["dict_col"].apply(unnest_dict)
age = [p[0] for p in data]
name = [p[1] for p in data]
print(age)
# [[1,2],[3,4]]
print(name)
# [["john","doe"],["foo","bar"]]
Isnt there a way to extract the series directly from the function?
NOTE: This is not the same question as this SO since that it is specific of how to explode a dict - my question is regarding how to extract two (or more) series directly from the function-return. The example provided is just an example, and could've been any operation.