如何通过函数在Pandas DataFrame中新增州提取与标题清理列?
Great question! Let's walk through how to solve this problem of pulling the state name from your title column and creating a cleaned version of the title without that state suffix.
First, let's refine your existing code for clarity and verify it works as intended:
import pandas as pd # Create your sample DataFrame df = pd.Series([ 'Accommodation Payroll Employment in Texas', 'Accounting, Tax Preparation, Bookkeeping, and Payroll Services Payroll Employment in Texas' ]).to_frame() df.columns = ['title'] def extract_state_and_clean_title(row): title_clean = None state = None # Check for ' in ' separator (primary case) split_by_in = row['title'].split(' in ') if len(split_by_in) == 2: state = split_by_in[1] title_clean = split_by_in[0] elif len(split_by_in) == 3: # Handle cases where 'in' appears within the title text itself state = split_by_in[2] title_clean = ' in '.join(split_by_in[:2]) # Fallback to ' for ' separator if ' in ' doesn't fit else: split_by_for = row['title'].split(' for ') if len(split_by_for) == 2: state = split_by_for[1] title_clean = split_by_for[0] return title_clean, state # Apply the function to create your new columns df[['title_clean', 'state']] = df.apply(extract_state_and_clean_title, axis=1) # View the final result print(df)
Sample Output:
title title_clean state 0 Accommodation Payroll Employment in Texas Accommodation Payroll Employment Texas 1 Accounting, Tax Preparation, Bookkeeping, and Payroll Services... Accounting, Tax Preparation, Bookkeeping, and Payroll Serv... Texas
Key Refinements & Explanations:
- Renamed the function to be more descriptive (
extract_state_and_clean_titleinstead ofstate_code) for better readability. - Used
' in '.join(split_by_in[:2])instead of manual string concatenation, which is cleaner and handles any intermediate 'in' occurrences in the title. - Added comments to explain each logical step, making the code easier to maintain later.
Optimization for Large Datasets:
If you're working with a big dataset, using apply() can be slow since it processes rows one by one. A faster, vectorized approach uses regex with str.extract() and str.replace():
import pandas as pd df = pd.Series([ 'Accommodation Payroll Employment in Texas', 'Accounting, Tax Preparation, Bookkeeping, and Payroll Services Payroll Employment in Texas', 'Retail Job Openings for California' ]).to_frame() df.columns = ['title'] # Extract state using regex that matches either ' in ' or ' for ' suffix df['state'] = df['title'].str.extract(r'(?: in | for )(\w+)$', expand=False) # Clean title by removing the state suffix df['title_clean'] = df['title'].str.replace(r' in \w+$| for \w+$', '', regex=True) print(df)
This regex approach is significantly faster for large data and handles both separator cases in one line. The pattern (?: in | for )(\w+)$ targets the state name at the end of the string, while the replacement pattern strips that suffix from the original title.
内容的提问来源于stack exchange,提问作者Kyle

