实用脚本统计定位球得分占比多少?

wen 实用脚本 3

本文目录导读:

实用脚本统计定位球得分占比多少?

  1. 核心定义
  2. 场景一:你有比赛数据表(Excel/CSV)
  3. 场景二:你有逐场比赛的基础比分(没有进球方式明细)
  4. 场景三:使用足球API(如 API-Football 或 Opta)实时抓取
  5. 进阶:可视化与趋势分析
  6. 注意事项(统计陷阱)

在足球数据分析中,统计定位球得分占比可以帮助判断一支球队的进攻手段是否依赖定位球,或者防守端是否容易在定位球上丢球。

这里提供几个不同场景的实用Python脚本逻辑,可以根据你的数据源(是Excel/CSV,还是API)选择使用。

核心定义

先明确概念,避免统计口径误差:

  • 定位球(Set Piece):通常指 任意球(直接/间接)、角球点球界外球(掷入禁区后破门),通常不包括运动战中的反击或传中。
  • 占比公式(定位球进球数 / 总进球数) * 100%

你有比赛数据表(Excel/CSV)

这是最常见的情况,假设你有一份包含每场比赛进球细节的数据表。

假设数据格式(CSV 列名)match_id, team, goal_minute, player, goal_type goal_type 列包含:Open Play(运动战), Corner(角球), Free Kick(任意球), Penalty(点球), Throw-in(界外球)。

import pandas as pd
# 1. 加载数据
df = pd.read_csv('goals_data.csv')
# 2. 定义定位球类型
set_piece_types = ['Corner', 'Free Kick', 'Penalty', 'Throw-in']
# 3. 计算总进球数(假设每行代表一个进球)
total_goals = len(df)
# 4. 筛选出定位球进球
set_piece_df = df[df['goal_type'].isin(set_piece_types)]
set_piece_goals = len(set_piece_df)
# 5. 计算占比
if total_goals > 0:
    ratio = (set_piece_goals / total_goals) * 100
    print(f"总进球数: {total_goals}")
    print(f"定位球进球数: {set_piece_goals}")
    print(f"定位球得分占比: {ratio:.2f}%")
else:
    print("无进球数据")
# 额外:按球队分组统计
team_stats = df.groupby('team').apply(
    lambda x: pd.Series({
        '总进球': len(x),
        '定位球进球': len(x[x['goal_type'].isin(set_piece_types)]),
        '占比': (len(x[x['goal_type'].isin(set_piece_types)]) / len(x) * 100) if len(x) > 0 else 0
    })
).reset_index()
print("\n按球队统计:")
print(team_stats)

你有逐场比赛的基础比分(没有进球方式明细)

如果你的数据只有“进球总数”和“定位球进球数”两列,脚本更简单。

假设数据格式match_id, team, total_goals_scored, set_piece_goals_scored

import pandas as pd
# 加载数据
df = pd.read_csv('match_stats.csv')
# 按球队聚合
team_total = df.groupby('team')['total_goals_scored'].sum()
team_set_piece = df.groupby('team')['set_piece_goals_scored'].sum()
# 计算占比
result = pd.DataFrame({
    '总进球': team_total,
    '定位球进球': team_set_piece
})
result['定位球占比'] = (result['定位球进球'] / result['总进球'] * 100).round(2)
# 处理除零情况(将总进球为0的占比设为0)
result['定位球占比'] = result['定位球占比'].fillna(0)
print(result)

使用足球API(如 API-Football 或 Opta)实时抓取

如果你使用的是实时数据接口,通常返回JSON格式,可以直接在代码里处理。

import requests
import pandas as pd
# 示例接口(需要替换为你的API密钥和真实URL)
url = "https://v3.football.api-sports.io/fixtures/statistics"
headers = {
    'x-apisports-key': 'YOUR_API_KEY'
}
params = {
    'fixture': '1085847'  # 比赛ID
}
response = requests.get(url, headers=headers, params=params)
data = response.json()
# 假设响应中有 goals 细节
# 这里简化为从某个字段提取进球类型
goals = data['response'][0]['goals']  # 根据实际API结构调整
total_goals = len(goals)
set_piece_count = 0
for goal in goals:
    # 假设有 'type' 字段,或者 'assist' 字段
    goal_type = goal.get('type', '')
    if goal_type in ['Penalty', 'Free Kick', 'Corner', 'Throw-in']:
        set_piece_count += 1
print(f"定位球占比: {set_piece_count/total_goals*100:.2f}%")

进阶:可视化与趋势分析

如果要做跨赛季的趋势分析,可以用 matplotlib 画出折线图,看占比是上升还是下降。

import matplotlib.pyplot as plt
# 假设 df 包含 'season' 和 'set_piece_ratio' 两列
plt.figure(figsize=(12, 6))
plt.plot(df['season'], df['set_piece_ratio'], marker='o', linestyle='-')'历史赛季定位球得分占比趋势')
plt.xlabel('赛季')
plt.ylabel('占比 (%)')
plt.grid(True, alpha=0.3)
plt.show()

注意事项(统计陷阱)

  1. “点球”是否算作定位球?
    • 严格统计(如 Opta 标准):点球是算的,因为是死球状态。
    • 战术统计:有些教练喜欢分开看,因为点球和任意球/角球的战术含义不同,建议在输出结果时,单独列出“点球占比”,避免误导。
  2. “反击战术”与“定位球战术”:为了准确,你的数据源需要能区分是由对方失误导致的运动战进球,还是从定位球直接发的战术。

抱歉,评论功能暂时关闭!