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

如何用Python函数自动生成DataFrame球员总数据列

自动化计算足球球员总数据的函数实现

针对你的需求,这里提供一个简洁高效的函数,自动将每90分钟统计列转换为总数据列:

import pandas as pd

def calculate_total_player_stats(df):
    # 获取所有包含"/90"的统计列
    col_90s = [col for col in df.columns if '/90' in col]
    
    # 遍历处理每一列
    for col in col_90s:
        # 生成新列名:移除"/90"部分并清理多余空格
        new_col_name = col.split('/90')[0].strip()
        # 计算总数据:每90分钟数据 × 出场90分钟单位数
        df[new_col_name] = df[col] * df['90s']
    
    return df

使用方法

直接传入你的DataFrame即可完成转换:

# 假设你的原始数据框名为player_df
player_df = calculate_total_player_stats(player_df)

细节说明

  • 用列表推导式替代原循环,代码更简洁且执行效率更高
  • split('/90')[0].strip()能兼容列名中"/90"前带空格的情况(比如Pases recibidos /90会被转换为Pases recibidos)
  • 原列中的NaN值会被保留,相乘后结果仍为NaN,符合统计逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 17:01:35