Pandas分组拟合线性回归生成系数Series时触发KeyError问题
Hey there! Let's break down why you're hitting that KeyError: ('bid_cpc', 'occurred at index bid') and get your coefficient series working smoothly.
Common Causes of the Error
This error almost always stems from a mismatch between the structure your group fitting function returns and how you're trying to extract coefficients. The most likely issues are:
- Your fitting function returns an unlabeled array or a Series with mismatched indices
- You're trying to access a column/key that doesn't exist in the grouped results
- The grouped output has a multi-level index you haven't accounted for
Step-by-Step Solution
Let's walk through corrected code for each step, starting with the critical fitting function:
1. Define a Robust Fitting Function
Make sure your function returns a clearly labeled Series so we can easily pull coefficients later. Replace target_column with your actual dependent variable column name:
from sklearn.linear_model import LinearRegression import pandas as pd def fit_market_regression(group): # Keep X as a 2D array (double brackets) to avoid sklearn warnings X = group[['bid_cpc']] y = group['target_column'] # Skip groups with too few samples to avoid fitting failures if len(X) < 2: return pd.Series({'coefficient': None, 'intercept': None}) model = LinearRegression() model.fit(X, y) # Return labeled values for easy extraction return pd.Series({ 'coefficient': model.coef_[0], # Pull the single coefficient for 'bid_cpc' 'intercept': model.intercept_ })
2. Apply the Function to Your Groups
# Assume your original DataFrame is named 't' groups = t.groupby(by="market") model_results = groups.apply(fit_market_regression)
This gives you a clean DataFrame where each row is a market, and columns are coefficient and intercept.
3. Create the Target Coefficient Series
Now you can directly extract the coefficient column into a Series with market as the index:
coefficient_series = model_results['coefficient']
Why Your Original Code Failed
Chances are your original fitting function did one of these:
- Returned an unlabeled numpy array (like just
model.coef_) instead of a named Series. When applied to groups, this creates a Series where each element is an array—trying to access 'bid_cpc' here will throw a KeyError because the array has no named keys. - Returned a Series indexed by feature names (e.g.,
pd.Series(model.coef_, index=['bid_cpc'])). While this works, you need to explicitly pull the 'bid_cpc' column from the grouped DataFrame, not treat the entire result as a flat Series.
Quick Validation Check
Run print(model_results.head())—you should see a straightforward DataFrame with market as the index and your coefficient/intercept columns. If you see nested arrays or multi-level indices, adjust the fitting function to return a properly labeled Series.
内容的提问来源于stack exchange,提问作者Ladenkov Vladislav

