I have the following dataframe,
df = pd.DataFrame({'a':[[1,2],[2,3],[1]],'b':[2,4,1]})
which looks like,
a b
0 [1, 2] 2
1 [2, 3] 4
2 [1] 1
Now, I want to add a column in this dataframe whose elements are that value in each list of column ‘a’ whose absolute difference with corresponding value in column ‘b’ is minimum.
i.e. I want to add a column ‘c’ in my dataframe, such that my df becomes,
a b c
0 [1,2] 2 2
1 [2,3] 4 3
2 [1] 1 1
I tried using lambda function, but couldn’t do it.
>Solution :
You can iterate through the column using zip and store the index of min absolute result.
import numpy as np
import pandas as pd
df = pd.DataFrame({'a':[[1,2],[2,3],[1]],'b':[2,4,1]})
c_col = []
for x, y in zip(df['a'], df['b']):
array = abs(np.array(x)-y)
c_col.append(x[array.argmin(axis=0)])
df['c'] = c_col
print(df)
Output :
a b c
0 [1, 2] 2 2
1 [2, 3] 4 3
2 [1] 1 1