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?

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]

Leave a Reply