I have two columns in a Pandas Dataframe:
value_1 values_2
10 [{'name': 'eric', 'count':20}, {'name': 'john', 'count':30}]
20 [{'name': 'james', 'count':20}, {'name': 'jamie', 'count':35}]
I would like to create a function that creates 2 columns in the same dataframe. Here is what I try to have in the function:
- Calculates differences in each row between 'count' key values in column values_2 and value in column 'value_1'.
- In the new columns keep only the lowest name difference, and in the other the lowest difference value. example for row 1: for eric 20 - 10 and for john 30 - 10 The lowest is for eric, I would like to give his name as a value in a new column and the lowest difference value in another.
Expected output:
value_1 values_2 lowest lowest_difference
10 [{'name': 'eric', 'count':20}, {'name': 'john', 'count':30}] eric 10 (i.e. 20 -10)
20 [{'name': 'james', 'count':20}, {'name': 'jamie', 'count':35}] james 0 (i.e. 20-20)
I know I can do "apply" and/or "for loops" but don't know how do it in an elegant way. How can I do it ?