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

How to restore values between other values in Pandas

I have dataframe:

 import pandas as pd
    data = {'col1':[[0,1,2,3,4,5],[0,1,2,3,4,5,6,7,8,9],[0,1,2,3]],
           'col_2':[[2,4,5],[6,7,9],[0,1,2]]}
    
    df = pd.DataFrame(data)
            col1                           col_2
    0   [0, 1, 2, 3, 4, 5]              [2, 4, 5]
    1   [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]  [6, 7, 9]
    2   [0, 1, 2, 3]                    [0, 1, 2]

I would like to restore the values between the elements for the column col_2 from col_1 and get results in col_3

            col1                      col_2        col_3
0   [0, 1, 2, 3, 4, 5]              [2, 4, 5]   [2, 3, 4, 5]
1   [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]  [6, 7, 9]   [6, 7, 8, 9]
2   [0, 1, 2, 3]                    [0, 1, 2]    [0, 1, 2]

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

>Solution :

I think this should work:

df['col_3'] = df.apply(lambda row: [i for i in row['col_1'] if i >= min(row['col_2']) and i <= max(row['col_2'])], axis=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