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

使用Pandas处理电影数据集:移除预算含?的条目并执行回归分析

Fixing Your Movie Dataset Budget Cleaning & Regression Workflow

Hey Christina, let's walk through how to fix this properly—your initial approach with a while loop isn't the right fit for Pandas' vectorized operations, so let's replace that with efficient, idiomatic code.

Step 1: Remove Entries with "?" in the Budget Field

First, we'll filter out any rows where budget contains "?". Pandas makes this easy with boolean indexing. You have two solid options here:

  • If "?" is the entire value in the budget field:

    # Keep only rows where budget isn't exactly "?"
    df = df[df['budget'] != '?']
    
  • If "?" could be part of a longer string (e.g., "1000?") and you want to exclude those too:

    # Use str.contains to find any row with "?", then invert the mask with ~
    df = df[~df['budget'].str.contains('\?', regex=True)]
    

Step 2: Convert Budget to Integer Type

Once we've removed the "?" entries, we can safely convert the budget column to integers. For extra robustness, use pd.to_numeric (it handles edge cases better than astype):

# Convert to numeric, coerce any remaining invalid values to NaN
df['budget'] = pd.to_numeric(df['budget'], errors='coerce')

# Drop any rows that ended up with NaN from invalid values (just in case)
df = df.dropna(subset=['budget'])

# Finally, convert to integer
df['budget'] = df['budget'].astype(int)

Step 3: Run Regression Analysis with the Quality Field

Now that your budget column is clean, you can perform regression using a library like scikit-learn. Here's a quick example of linear regression:

from sklearn.linear_model import LinearRegression

# Prepare your features and target variable
X = df[['budget']]  # Scikit-learn expects 2D feature arrays
y = df['quality']

# Initialize and fit the model
model = LinearRegression()
model.fit(X, y)

# Print key results
print(f"Regression Intercept: {model.intercept_:.2f}")
print(f"Budget Coefficient: {model.coef_[0]:.2f}")

Why Your Initial Code Failed

  • Pandas DataFrames don't have a remove() method—you were calling a non-existent function.
  • Using a while loop to iterate through rows is inefficient and unidiomatic for Pandas. Always prefer vectorized operations (like boolean indexing) over loops for DataFrame manipulation, as they're faster and more readable.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 07:07:50