Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding a column in Dataframe with a specific property in python

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,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading