I keep getting ‘function not iterable’ error with Pandas DataFrame filter, but same function works with apply. What am I missing?

TypeError: 'function' object is not iterable
I keep getting this error when I feel that the filter function would achieve my desired result if only it would work !..

What I’d like to know is… What am I missing when I decide to use ‘filter’, (which to me looks neater than using ‘loc’ to achieve the same outcome), and almost always end up with this error.

Here’s a simple example.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(4,4), columns=['a', 'b', 'c', 'd'])
df.filter(lambda x:x['b'] > 0.5, axis=1)
Traceback (most recent call last):
Cell In[190], line 1
df.filter(lambda x:x['b'] > 0.5, axis=1)
File ~/.local/lib/python3.8/site-packages/pandas/core/generic.py:5454 in filter
return self.reindex(**{name: [r for r in items if r in labels]})
TypeError: 'function' object is not iterable
In [1]: df.apply(lambda x:x['b'] > 0.5, axis=1)
Out[1]:
0 True
1 True
2 False
3 False
dtype: bool
In [2]: df.loc[df['b']> 0.5]
Out[2]:
a b c d
0 0.578342 0.82178 0.414652 0.630859
1 0.181232 0.91806 0.772260 0.502921
</code>
<code>import pandas as pd import numpy as np df = pd.DataFrame(np.random.rand(4,4), columns=['a', 'b', 'c', 'd']) df.filter(lambda x:x['b'] > 0.5, axis=1) Traceback (most recent call last): Cell In[190], line 1 df.filter(lambda x:x['b'] > 0.5, axis=1) File ~/.local/lib/python3.8/site-packages/pandas/core/generic.py:5454 in filter return self.reindex(**{name: [r for r in items if r in labels]}) TypeError: 'function' object is not iterable In [1]: df.apply(lambda x:x['b'] > 0.5, axis=1) Out[1]: 0 True 1 True 2 False 3 False dtype: bool In [2]: df.loc[df['b']> 0.5] Out[2]: a b c d 0 0.578342 0.82178 0.414652 0.630859 1 0.181232 0.91806 0.772260 0.502921 </code>
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(4,4), columns=['a', 'b', 'c', 'd'])
df.filter(lambda x:x['b'] > 0.5, axis=1)
Traceback (most recent call last):

  Cell In[190], line 1
    df.filter(lambda x:x['b'] > 0.5, axis=1)

  File ~/.local/lib/python3.8/site-packages/pandas/core/generic.py:5454 in filter
    return self.reindex(**{name: [r for r in items if r in labels]})

TypeError: 'function' object is not iterable

In [1]: df.apply(lambda x:x['b'] > 0.5, axis=1)
Out[1]: 
0     True
1     True
2    False
3    False
dtype: bool


In [2]: df.loc[df['b']> 0.5]
Out[2]: 
          a        b         c         d
0  0.578342  0.82178  0.414652  0.630859
1  0.181232  0.91806  0.772260  0.502921

Even the most basic lambda function creates an error, eg.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>In [5]: tru = lambda x: True
In [6]: df['b'].filter(tru)
Traceback (most recent call last):
Cell In[196], line 1
df['b'].filter(tru)
File ~/.local/lib/python3.8/site-packages/pandas/core/generic.py:5454 in filter
return self.reindex(**{name: [r for r in items if r in labels]})
TypeError: 'function' object is not iterable
Once again
In [7]: df['b'].apply(tru)
Out[7]:
0 True
1 True
2 True
3 True
Name: b, dtype: bool
</code>
<code>In [5]: tru = lambda x: True In [6]: df['b'].filter(tru) Traceback (most recent call last): Cell In[196], line 1 df['b'].filter(tru) File ~/.local/lib/python3.8/site-packages/pandas/core/generic.py:5454 in filter return self.reindex(**{name: [r for r in items if r in labels]}) TypeError: 'function' object is not iterable Once again In [7]: df['b'].apply(tru) Out[7]: 0 True 1 True 2 True 3 True Name: b, dtype: bool </code>
In [5]: tru = lambda x: True

In [6]: df['b'].filter(tru)
Traceback (most recent call last):

  Cell In[196], line 1
    df['b'].filter(tru)

  File ~/.local/lib/python3.8/site-packages/pandas/core/generic.py:5454 in filter
    return self.reindex(**{name: [r for r in items if r in labels]})

TypeError: 'function' object is not iterable

Once again
In [7]: df['b'].apply(tru)
Out[7]: 
0    True
1    True
2    True
3    True
Name: b, dtype: bool

In my mind, filter requires a function to return a bool, which the simplistic example clearly does, yet ‘filter’ fails and again ‘apply’ works.
I don’t see why a function is not iterable for filter but still works with apply.
What is there ‘under the hood’ that makes the difference in outcome?

I’d be very grateful if some can explain and give me the ‘eureka’ moment of understanding so that I’ll not fall in this trap again.

As explained above, I tried df.filter(function) which failed, yet
df.loc[df.apply(function)] yields the correct result.

New contributor

John Tweed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật