如何为Pandas中待输出的行设置多重限制?
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
EMPFLAGis 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
NAICScolumn is string-typed (convert withdf['NAICS'] = df['NAICS'].astype(str)if needed) sostr.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 = 100then useEST > @min_estin the query)
Testing Your Filters
Always verify your results to make sure the filters work as expected. You can use:
filtered_df.shapeto check the number of rows/columns after filteringfiltered_df['FIPSTATE'].unique()to confirm only the desired states are includedfiltered_df['EMPFLAG'].value_counts(dropna=False)to ensure suppression flags are excluded
内容的提问来源于stack exchange,提问作者rcoffey96

