0
a=np.random.randint(10,size=(3,3))
b=np.random.randint(10,size=(3,3))
c=np.random.randint(10,size=(3,3))
d=[np.vstack([a,b,c])]
df=pd.DataFrame(d,columns=['a','b','c'])
def filter_func(x):
    return 0 if x<5 else 1 
df.groupby('a').filter(filter_func)

This's just an examle. I got the 'The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().' message. What am i doing wrong??

eser
  • 1
  • 1
  • Instead of 0 or 1, you could use True or False. `return False if x<5 else True` ? – shahkalpesh Apr 09 '21 at 09:52
  • Your code breaks in different ways than you described. – norok2 Apr 09 '21 at 09:54
  • Also, what do you expect filter to do? "Note that this routine does not filter a dataframe on its contents. The filter is applied to the labels of the index." https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.filter.html – norok2 Apr 09 '21 at 09:59
  • What is your desired output? – Daweo Apr 09 '21 at 10:03
  • Your code raise `ValueError: Must pass 2-d input. shape=(1, 9, 3)` first – azro Apr 09 '21 at 10:07
  • This is because `x` is dataframe. `<` can't compare dataframe. – Ynjxsjmh Apr 09 '21 at 10:09
  • Does this answer your question? [Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o) – Ynjxsjmh Apr 09 '21 at 10:10

0 Answers0