{"id":3539,"date":"2026-11-03T13:12:17","date_gmt":"2026-11-03T05:12:17","guid":{"rendered":"http:\/\/www.taxfizzle.com\/blog\/?p=3539"},"modified":"2026-11-03T13:12:17","modified_gmt":"2026-11-03T05:12:17","slug":"how-to-filter-a-pandas-dataframe-by-multiple-columns-4132-15b9af","status":"publish","type":"post","link":"http:\/\/www.taxfizzle.com\/blog\/2026\/11\/03\/how-to-filter-a-pandas-dataframe-by-multiple-columns-4132-15b9af\/","title":{"rendered":"How to filter a pandas DataFrame by multiple columns?"},"content":{"rendered":"<p>Hey there, data folks! Let\u2019s cut to the chase\u2014if you\u2019ve ever sat staring at a messy pandas DataFrame, wondering how to pull exactly the slice of data you need across multiple columns, you\u2019re not alone. I\u2019ve been in your shoes, tripping over wrong filter syntax and wasting hours sifting through garbage data that didn\u2019t fit my needs. And yeah, I run a filter supply company, but stick with me\u2014this isn\u2019t a sales pitch. It\u2019s real, actionable pandas tips that I actually use for our own data team (you know, the people who source the industrial filters we sell? They need clean data too). <a href=\"https:\/\/www.dswfiltration.com\/filter\/\">Filter<\/a><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.dswfiltration.com\/uploads\/44374\/small\/high-pressure-backwashing-filter-element-for18423.jpg\"><\/p>\n<p>Let\u2019s start with the basics. Pandas is that Python library every data nerd swears by for wrangling tabular data, right? Its DataFrame is like a big, organized spreadsheet\u2014except way cooler when you know how to filter it. Filtering by one column is easy (df[df[&#8216;age&#8217;] &gt; 30]), but when you add a second, third, or fifth condition? That\u2019s where people start panicking. Over the years, my team and I have tested every trick in the book, and I\u2019ve picked up hacks that actually work, no weird errors or syntax headaches.<\/p>\n<p>First off, let\u2019s get the core rule straight: when combining multiple conditions, you can\u2019t use plain \u2018and\u2019 or \u2018or\u2019. Wait, why? Because pandas uses bitwise operators for vectorized operations, not Python\u2019s logical operators. So \u2018and\u2019 becomes \u2018&amp;\u2019 and \u2018or\u2019 becomes \u2018|\u2019\u2014and super important: you HAVE to wrap each individual condition in parentheses. Skip the parens, and you\u2019ll get a nasty TypeError that makes zero sense. Let\u2019s test this with a real example that\u2019s relevant to my work. Let\u2019s say we have a DataFrame that tracks incoming filter shipments\u2014columns like \u2018filter_type\u2019, \u2018quantity\u2019, \u2018material\u2019, \u2018delivery_status\u2019, and \u2018warehouse_location\u2019. Let\u2019s say we want to pull all shipments where:<\/p>\n<ol>\n<li>They\u2019re HEPA filters (\u2018filter_type\u2019 == &#8216;HEPA&#8217;)<\/li>\n<li>Quantity is over 100 (\u2018quantity\u2019 &gt; 100)<\/li>\n<li>They\u2019re not marked \u201cdamaged\u201d (\u2018delivery_status\u2019 != &#8216;damaged&#8217;)<\/li>\n<\/ol>\n<p>That\u2019s three conditions, all across different columns. Let\u2019s write that first try, the wrong way, to show you what not to do:<br \/>\nbad_filter = df[df[&#8216;filter_type&#8217;] == &#8216;HEPA&#8217; and df[&#8216;quantity&#8217;] &gt; 100 and df[&#8216;delivery_status&#8217;] != &#8216;damaged&#8217;]<br \/>\nIf you run that, you\u2019ll get a TypeError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). Which is basically pandas yelling at you for forgetting parentheses and using \u2018and\u2019 instead of \u2018&amp;\u2019. The corrected version works like a charm:<br \/>\ngood_filter = df[(df[&#8216;filter_type&#8217;] == &#8216;HEPA&#8217;) &amp; (df[&#8216;quantity&#8217;] &gt; 100) &amp; (df[&#8216;delivery_status&#8217;] != &#8216;damaged&#8217;)]<br \/>\nBoom. That\u2019s simple, right? No extra libraries, no complicated syntax\u2014just parens and bitwise operators. I use this exact line every week to pull shipment data for our warehouse team.<\/p>\n<p>But wait, what if you have more conditions? Or conditions that are ranges, or multiple values in one column? That\u2019s where things get flexible. Let\u2019s say instead of just HEPA, we want HEPA and activated carbon filters, plus shipments over 50 and under 200, plus only locations in Chicago or Dallas. That\u2019s where .isin() comes in\u2014way cleaner than writing 5 separate equality checks. Let\u2019s rewrite that:<br \/>\nmulti_col_filter = df[(df[&#8216;filter_type&#8217;].isin([&#8216;HEPA&#8217;, &#8216;activated_carbon&#8217;])) &amp;<br \/>\n(df[&#8216;quantity&#8217;].between(50, 200)) &amp;<br \/>\n(df[&#8216;warehouse_location&#8217;].isin([&#8216;Chicago&#8217;, &#8216;Dallas&#8217;]))]<br \/>\nSee how much cleaner that is? Instead of typing (df[&#8216;filter_type&#8217;] == &#8216;HEPA&#8217;) | (df[&#8216;filter_type&#8217;] == &#8216;activated_carbon&#8217;), which also works but gets messy fast when you have 10 values, .isin() is perfect for when you want a column to match any of a list of values. And .between() is a lifesaver for range checks\u2014no need for (df[&#8216;quantity&#8217;] &gt;=50) &amp; (df[&#8216;quantity&#8217;] &lt;=200), though that works too, .between() is just more readable. My team uses this when we\u2019re auditing our inventory levels across different warehouse zones.<\/p>\n<p>Now, let\u2019s talk about when this gets more advanced, like if you need to filter columns where values are null? Or if you want to combine string conditions? Let\u2019s say we want all filter materials that start with \u201cpoly\u201d and have a delivery note that mentions \u201cinspected\u201d. We can use .str.contains() for string conditions, and combine it with our bitwise operators:<br \/>\nstring_filter = df[(df[&#8216;material&#8217;].str.startswith(&#8216;poly&#8217;)) &amp;<br \/>\n(df[&#8216;delivery_notes&#8217;].str.contains(&#8216;inspected&#8217;, na=False))]<br \/>\nThe na=False part is important here\u2014it skips null values in \u2018delivery_notes\u2019 instead of throwing an error, which saves so much debugging time. I almost forgot that a few months ago, when a shipment had a missing delivery note and the whole filter broke\u2014now I never skip that parameter.<\/p>\n<p>Wait, what if you\u2019re working with really large DataFrames? Like, we\u2019re talking hundreds of thousands of shipment records, not just a tiny test set. The methods above are all vectorized operations, which means they\u2019re fast\u2014way faster than looping through each row in Python. I\u2019ve tested it: a loop would take 10 minutes to filter 500k rows, but the vectorized method takes less than a second. That\u2019s a game-changer for my data team, who have to pull real-time inventory data for sales and operations.<\/p>\n<p>Now, let\u2019s chat about a common pitfall: the all-vs-any mistake. If you\u2019re using .isin() or multiple conditions, remember that \u2018&amp;\u2019 is \u201call conditions must be true\u201d (that\u2019s pandas\u2019 version of \u2018and\u2019) and \u2018|\u2019 is \u201cany condition can be true\u201d (pandas\u2019 version of \u2018or\u2019). I see people mix these up all the time\u2014last month, a new intern at my company ran a filter that used \u2018|\u2019 instead of \u2018&amp;\u2019 for the shipment conditions, and they accidentally pulled in 2x more orders than needed, leading to extra warehouse pullers working overtime. Oops. So always double-check which operator you\u2019re using, depending on if you want all conditions met or just any.<\/p>\n<p>Another trick: if you have a ton of conditions, and writing them all in one line gets messy, you can use query() method. That\u2019s another way to filter multiple columns, and it reads almost like plain English. Let\u2019s take our original HEPA filter example and rewrite it with query():<br \/>\nquery_filter = df.query(&quot;filter_type == &#8216;HEPA&#8217; &amp; quantity &gt; 100 &amp; delivery_status != &#8216;damaged&#8217;&quot;)<br \/>\nThat\u2019s way more readable, right? Like, almost no pandas syntax to learn\u2014just write the conditions as a string. It\u2019s especially helpful for people who are new to pandas, or when you have a lot of conditions and don\u2019t want to stare at a wall of parentheses. The downside? It\u2019s slightly slower than the bracket method for super large DataFrames, but for most use cases, it\u2019s totally fine. I use query() when I\u2019m sharing code with non-technical stakeholders, because it\u2019s easier for them to follow along.<\/p>\n<p>Let\u2019s put this all together with a real, full example from our work. Suppose our filter shipment DataFrame looks like this:<br \/>\nfilter_type | quantity | material | delivery_status | warehouse_location | delivery_notes<br \/>\nHEPA        | 120      | fiberglass | good | Chicago | inspected<br \/>\nactivated_carbon | 80 | carbon | good | Dallas | inspected<br \/>\nHEPA        | 50 | fiberglass | damaged | Chicago | needs replacement<br \/>\nactivated_carbon | 200 | carbon | good | Austin | no notes<br \/>\nHEPA        | 150 | pete | good | Dallas | inspected<\/p>\n<p>If we use our first bracket method:<br \/>\nresult = df[(df[&#8216;filter_type&#8217;] == &#8216;HEPA&#8217;) &amp; (df[&#8216;quantity&#8217;] &gt; 100) &amp; (df[&#8216;delivery_status&#8217;] != &#8216;damaged&#8217;)]<br \/>\nWhat do we get? Only the last row: HEPA filters, 150 quantity, good status, Dallas location. That\u2019s exactly what we wanted. If we used the .isin() method for a broader pull, we\u2019d get all HEPA or activated carbon filters over 50, in Chicago or Dallas, and so on.<\/p>\n<p>Now, a quick note: I\u2019m not here to sell you a filter, but here\u2019s the thing\u2014just like how we make industrial filters that stop exactly what you don\u2019t want (dust, debris, contaminants), pandas filter functions stop exactly the data you don\u2019t need. Both are about precision, right? If you\u2019ve ever struggled with messy data or inefficient filtering for your work, we at our filter supply company know how important precision is\u2014whether it\u2019s a air filter for a factory, or a data filter for your DataFrame.<\/p>\n<p>Wait, before you go, a couple of quick best practices to wrap this up: 1) Always test your filters on a small subset of data first, not the whole dataset. That way, if you make a mistake, you don\u2019t waste time or mess up big datasets. 2) Name your filtered DataFrames something obvious, like hepa_filter_100, not temp or result\u2014you\u2019ll thank yourself later when you\u2019re going back to code from a week ago. 3) If you\u2019re working with pandas 1.0+, all these methods work seamlessly\u2014no need for outdated versions.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.dswfiltration.com\/uploads\/44374\/small\/flanged-basket-strainer-vertical-stainless1e675.jpg\"><\/p>\n<p>If you\u2019re tired of fumbling with messy data, or need help building precise filters for your datasets (or even industrial filters for your operations, if that\u2019s your thing), we\u2019re here to help. Reach out to our team to discuss your needs, whether you\u2019re filtering shipment data for inventory or sourcing high-performance filters that meet your exact specs.<\/p>\n<p><a href=\"https:\/\/www.dswfiltration.com\/oil-filter-element\/\">Oil Filter Element<\/a> References:<br \/>\nWes McKinney. Python for Data Analysis: Data Wrangling with Pandas, NumPy, and IPython. O&#8217;Reilly Media, 2022.<br \/>\nPandas Documentation: Indexing and Selecting Data. pandas.pydata.org, accessed 2024.<br \/>\nJulia Evans. \u201cPandas Gotchas.\u201d Julia Evans Blog, 2023.<\/p>\n<hr>\n<p><a href=\"https:\/\/www.dswfiltration.com\/\">Xinxiang Deshengwei Filtration and Purification Equipment Co., Ltd.<\/a><br \/>As one of the most experienced filter suppliers in China, we also support customized service. We warmly welcome you to buy bulk high quality filter made in China here from our factory. If you have any enquiry about cooperation, please feel free to email us.<br \/>Address: No.5 Daoqing Road, Hongqi District, Xinxiang City, Henan Province, China.<br \/>E-mail: rushufeng@dswgl.com<br \/>WebSite: <a href=\"https:\/\/www.dswfiltration.com\/\">https:\/\/www.dswfiltration.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey there, data folks! Let\u2019s cut to the chase\u2014if you\u2019ve ever sat staring at a messy &hellip; <a title=\"How to filter a pandas DataFrame by multiple columns?\" class=\"hm-read-more\" href=\"http:\/\/www.taxfizzle.com\/blog\/2026\/11\/03\/how-to-filter-a-pandas-dataframe-by-multiple-columns-4132-15b9af\/\"><span class=\"screen-reader-text\">How to filter a pandas DataFrame by multiple columns?<\/span>Read more<\/a><\/p>\n","protected":false},"author":812,"featured_media":3539,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3502],"class_list":["post-3539","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-industry","tag-filter-42cc-1607a5"],"_links":{"self":[{"href":"http:\/\/www.taxfizzle.com\/blog\/wp-json\/wp\/v2\/posts\/3539","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.taxfizzle.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.taxfizzle.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.taxfizzle.com\/blog\/wp-json\/wp\/v2\/users\/812"}],"replies":[{"embeddable":true,"href":"http:\/\/www.taxfizzle.com\/blog\/wp-json\/wp\/v2\/comments?post=3539"}],"version-history":[{"count":0,"href":"http:\/\/www.taxfizzle.com\/blog\/wp-json\/wp\/v2\/posts\/3539\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.taxfizzle.com\/blog\/wp-json\/wp\/v2\/posts\/3539"}],"wp:attachment":[{"href":"http:\/\/www.taxfizzle.com\/blog\/wp-json\/wp\/v2\/media?parent=3539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.taxfizzle.com\/blog\/wp-json\/wp\/v2\/categories?post=3539"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.taxfizzle.com\/blog\/wp-json\/wp\/v2\/tags?post=3539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}