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 filter a pandas list of numerical values?

I have a df like this:

                     value_list  
0             [200000.00, 100.00, 25.00]                 
1                       [860000.00]                          
2                  [148000.00, 25.00]  

I want a new column filtered_list which will filter out values less than 100 and greater 10000 so expected results

                     value_list                              filtered_list
0             [200000.00, 100.00, 25.00]                     [100.00]
1                       [860000.00]                          []
2                 [148000.00, 25.00, 9500]                   [9500]

How can I do this?

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

What I’ve tried:

df['filtered_list'] = df['value_list'].apply(lambda x: (e for e in x if e > 100 and e < 10000 ))

But this returned a generator object. Please advise.

>Solution :

You can use [] to create a list instead of a generator ():

df['filtered_list'] = df['value_list'].apply(lambda x: [e for e in x if 100 <= e < 10000])
print(df)

# Output
                value_list filtered_list
0  [200000.0, 100.0, 25.0]       [100.0]
1               [860000.0]            []
2   [148000.0, 25.0, 9500]        [9500]
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