I have values in a group and if the value falls within the range 5 to 25, then I want to keep this group in the data.
Based on Pandas: remove group from the data when a value in the group meets a required condition, I wrote this:
dfnew = df.groupby('groupname').filter(lambda x: (x['column2']>=5) & (x['column2']<=25))
When I use this, there's this error:
filter function returned a Series, but expected a scalar bool
Then I also tried:
dfnew = df.groupby('groupname').filter(lambda x: 5<= x['column2']<=25)
But it gave error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
And then I tried:
dfnew = df.groupby('groupname').filter(lambda x: (x['column2'].any()>=5) & (x['column2'].any()<=25))
Which just returns an empty dataframe with the column names
I am very new to python and datascience (literally coded for a few days). Please explain what's going on and help! Thank you so much!!