I have a data frame consisting of lists as elements. I want to subtract a value from each list and find the index of the minimum. I want to find the value corresponding to each list in another column.
My code:
df = pd.DataFrame({'A':[[1,2,3],[1,3,5,6]]})
df
A B
0 [1, 2, 3] [10, 20, 30]
1 [1, 3, 5, 6] [10, 30, 50, 60]
# lets subtract 2 from A, find index of minimum in this result and find corresponding element in the B column
val = 2
df['A_new_min'] = (df['A'].map(np.array)-val).map(abs).map(np.argmin)
df['B_new'] = df[['A_new_min','B']].apply(lambda x: x[1][x[0]],axis=1)
Present solution: It produces a correct solution but I don’t to want to store the A_new_min and it is unnecessary. I am looking if it is possible to get this result in one line of code?
df =
A B A_new_min B_new
0 [1, 2, 3] [10, 20, 30] 1 20
1 [1, 3, 5, 6] [10, 30, 50, 60] 0 10
Expected solution:
How can I obtain the below solution directly without having to create an additional and unnecessary column A_new_min? In simple words, I would like to
df =
A B B_new
0 [1, 2, 3] [10, 20, 30] 20
1 [1, 3, 5, 6] [10, 30, 50, 60] 10
>Solution :
With apply:
df["B_new"] = df.apply(lambda row: row["B"][np.argmin(abs(np.array(row["A"])-val))], axis=1)
>>> df
A B B_new
0 [1, 2, 3] [10, 20, 30] 20
1 [1, 3, 5, 6] [10, 30, 50, 60] 10