You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Pandas中待输出的行设置多重限制?

How to Apply Multiple Row Filters to Your Pandas DataFrame

Hey there! Let's tackle how to apply multiple row filters to your Pandas DataFrame based on the columns you've loaded from the CBP dataset. I'll walk you through two straightforward, practical methods that work perfectly for this scenario.

Method 1: Combine Boolean Conditions Directly

This is the most intuitive approach—you define individual filter conditions and combine them using logical operators. Let's use real-world examples tied to your columns:

Suppose you want to filter rows that meet all these criteria:

  • State code (FIPSTATE) is '06' (California)
  • NAICS industry code falls under manufacturing (starts with '31', '32', or '33')
  • Legal organization form (LFO) is '1' (Sole Proprietorship)
  • No employment data suppression (suppression flag EMPFLAG is empty)
  • Total establishments (EST) exceed 100

Here's how to code that:

import pandas as pd

# Load your data (same as your original code)
df = pd.read_csv('cbp15st.txt', delimiter=',', encoding='utf-8-sig')

# Define individual filter conditions
condition_state = df['FIPSTATE'] == '06'
condition_industry = df['NAICS'].str.startswith(('31', '32', '33'))
condition_legal = df['LFO'] == '1'
condition_no_suppression = df['EMPFLAG'].isna()
condition_large_est = df['EST'] > 100

# Combine all conditions with & (AND) - wrap each condition in parentheses to avoid precedence issues
filtered_df = df[condition_state & condition_industry & condition_legal & condition_no_suppression & condition_large_est]

# Optional: Check the result
print(filtered_df.head())

Key Notes for This Method:

  • Use & for AND logic, | for OR logic, and ~ for NOT (e.g., ~(df['LFO'] == '2') to exclude partnerships)
  • For multi-value matches (like multiple states), use isin(): df['FIPSTATE'].isin(['06', '12', '48'])
  • Ensure your NAICS column is string-typed (convert with df['NAICS'] = df['NAICS'].astype(str) if needed) so str.startswith() works
  • If suppression flags use a specific value (e.g., 'S' instead of NaN), adjust the condition to df['EMPFLAG'] != 'S'

Method 2: Use query() for Cleaner Syntax

If you prefer a more readable, SQL-like syntax, Pandas' query() method is perfect. It lets you write filters as plain text without repeating df['column'] every time.

Using the same criteria as above, here's the query() version:

filtered_df = df.query(
    "FIPSTATE == '06' "
    "and NAICS.str.startswith(('31', '32', '33')) "
    "and LFO == '1' "
    "and EMPFLAG.isna() "
    "and EST > 100"
)

Why Use query()?

  • It's easier to read for complex filter chains
  • You can reference variables from your environment (e.g., min_est = 100 then use EST > @min_est in the query)

Testing Your Filters

Always verify your results to make sure the filters work as expected. You can use:

  • filtered_df.shape to check the number of rows/columns after filtering
  • filtered_df['FIPSTATE'].unique() to confirm only the desired states are included
  • filtered_df['EMPFLAG'].value_counts(dropna=False) to ensure suppression flags are excluded

内容的提问来源于stack exchange,提问作者rcoffey96

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:32:49