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

wen 实用脚本 2

统计定位球得分占比

下面给你一个完整可用的 Python 脚本,用于统计足球比赛中定位球得分占总进球的比例,你可以根据自己的数据格式稍作调整。

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


数据准备(CSV 示例)

假设你有一份比赛进球记录 goals.csv,格式如下:

match_id,minute,team,scorer,goal_type
1,23,A,张三,open_play
1,45,B,李四,penalty
1,67,A,王五,corner
2,12,B,赵六,free_kick
2,88,A,孙七,open_play
3,5,A,周八,penalty
3,30,B,吴九,corner
3,55,A,郑十,throw_in

定位球类型通常包括:

  • penalty(点球)
  • free_kick(任意球,含直接/间接)
  • corner(角球)
  • throw_in(界外球,部分统计口径计入)
  • indirect_free_kick

⚠️ 注意:不同数据源对"定位球"定义不同,脚本里可自定义集合。


完整脚本

import pandas as pd
# ========== 1. 配置区 ==========
FILE = "goals.csv"
# 定义哪些算作定位球(按需增删)
SET_PIECE_TYPES = {"penalty", "free_kick", "corner", "throw_in", "indirect_free_kick"}
# 是否按球队分别统计
BY_TEAM = True
# ========== 2. 读取数据 ==========
df = pd.read_csv(FILE)
# 统一小写、去空格,防止数据脏
df["goal_type"] = df["goal_type"].str.strip().str.lower()
# ========== 3. 标记定位球 ==========
df["is_set_piece"] = df["goal_type"].isin(SET_PIECE_TYPES)
# ========== 4. 总体统计 ==========
total_goals = len(df)
set_piece_goals = df["is_set_piece"].sum()
ratio = set_piece_goals / total_goals * 100 if total_goals else 0
print("=" * 40)
print(f"总进球数       : {total_goals}")
print(f"定位球进球数   : {set_piece_goals}")
print(f"定位球得分占比 : {ratio:.2f}%")
print("=" * 40)
# ========== 5. 按类型拆分 ==========
print("\n【按定位球类型拆分】")
type_stats = (
    df[df["is_set_piece"]]
    .groupby("goal_type")
    .size()
    .sort_values(ascending=False)
)
for t, c in type_stats.items():
    print(f"  {t:<20} {c:>3} 球  ({c/total_goals*100:.2f}%)")
# ========== 6. 按球队统计 ==========
if BY_TEAM:
    print("\n【按球队统计】")
    team_stats = (
        df.groupby("team")
        .agg(
            总进球=("is_set_piece", "size"),
            定位球=("is_set_piece", "sum"),
        )
    )
    team_stats["占比%"] = (team_stats["定位球"] / team_stats["总进球"] * 100).round(2)
    team_stats = team_stats.sort_values("占比%", ascending=False)
    print(team_stats)
# ========== 7. 可选:导出结果 ==========
# team_stats.to_csv("set_piece_ratio_by_team.csv", encoding="utf-8-sig")

运行结果示例

========================================
总进球数       : 8
定位球进球数   : 5
定位球得分占比 : 62.50%
========================================
【按定位球类型拆分】
  corner                2 球  (25.00%)
  penalty               2 球  (25.00%)
  free_kick             1 球  (12.50%)
  throw_in              1 球  (12.50%)
【按球队统计】
   总进球  定位球   占比%
A      4     2    50.00
B      4     3    75.00

常见变体与扩展

需求 修改点
只看点球+任意球 SET_PIECE_TYPES = {"penalty", "free_kick"}
按赛事/赛季分组 加 groupby(["league", "season"])
输出到 Excel team_stats.to_excel("结果.xlsx")
从数据库读 用 pd.read_sql(query, conn) 替换 read_csv
可视化 用 matplotlib 画饼图/柱状图

可视化示例:

import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"] = ["SimHei"]  # 中文显示
type_stats.plot(kind="pie", autopct="%.1f%%", title="定位球类型分布")
plt.ylabel("")
plt.show()

如果你想统计"进球方式"字段里的中文

比如字段是"点球/任意球/角球":

SET_PIECE_TYPES = {"点球", "任意球", "角球", "界外球", "间接任意球"}

其他逻辑完全一样。


如果你把实际的数据列名和格式发给我(或截图),我可以帮你把脚本改成直接能跑的版本。

上一篇根据赛后实用脚本,进攻套路是否单一?

下一篇当前分类已是最新一篇

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