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.
CodePudding user response:
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]
CodePudding user response:
I would avoid apply
and use a double listcomp. So, assuming (df
) is your dataframe, use :
df["filtered_list"] = [[n for n in l if 100 <= n <= 10000]
for l in df["value_list"]]
Output:
print(df)
value_list filtered_list
0 [200000.0, 100.0, 25.0] [100.0]
1 [860000.0] []
2 [148000.0, 25.0, 9500] [9500]