Python 分析两队拦截抢断数据案例
下面我用一个完整的案例,展示如何用 Python 查看和分析两支球队的拦截、抢断数据。

准备数据
假设有一份足球比赛数据(可用 pandas 模拟):
import pandas as pd
import matplotlib.pyplot as plt
# 模拟两队球员的技术统计数据
data = {
'球队': ['A队','A队','A队','A队','A队',
'B队','B队','B队','B队','B队'],
'球员': ['张三','李四','王五','赵六','钱七',
'孙八','周九','吴十','郑一','冯二'],
'拦截': [3, 5, 2, 4, 1,
4, 6, 3, 2, 5],
'抢断': [2, 4, 3, 1, 5,
3, 2, 4, 6, 1]
}
df = pd.DataFrame(data)
print(df)
按球队汇总
# 按球队分组统计总和、均值
team_stats = df.groupby('球队')[['拦截','抢断']].agg(['sum','mean'])
print(team_stats)
输出示例:
拦截 抢断
sum mean sum mean
球队
A队 15 3.0 15 3.0
B队 20 4.0 16 3.2
更清晰的写法:
team_sum = df.groupby('球队')[['拦截','抢断']].sum()
team_mean = df.groupby('球队')[['拦截','抢断']].mean()
print("=== 总数据 ===")
print(team_sum)
print("\n=== 场均数据 ===")
print(team_mean.round(2))
对比可视化
plt.rcParams['font.sans-serif'] = ['SimHei'] # 中文显示
plt.rcParams['axes.unicode_minus'] = False
team_sum.plot(kind='bar', figsize=(8,5), color=['#4C72B0','#DD8452'])'两队拦截 vs 抢断总数对比')
plt.ylabel('次数')
plt.xticks(rotation=0)
plt.legend(['拦截','抢断'])
plt.tight_layout()
plt.show()
找出关键球员
# 找出每队拦截+抢断最多的球员
df['防守贡献'] = df['拦截'] + df['抢断']
top_players = df.sort_values('防守贡献', ascending=False).head(5)
print(top_players[['球队','球员','拦截','抢断','防守贡献']])
真实场景:读取比赛数据
如果数据来自 CSV 或 API:
# 从 CSV 读取
df = pd.read_csv('match_stats.csv')
# 从足球数据API(如 football-data.org, API-Football)获取
import requests
headers = {'X-Auth-Token': '你的API_KEY'}
url = 'https://api.football-data.org/v4/matches/123456'
resp = requests.get(url, headers=headers).json()
# 提取两队统计
home = resp['statistics']['home']
away = resp['statistics']['away']
进阶分析建议
| 分析维度 | 方法 |
|---|---|
| 场均对比 | groupby().mean() |
| 主客场差异 | 加 场地 列后分组 |
| 时间趋势 | 多轮比赛数据 plt.plot() 折线图 |
| 对手强度 | 结合对手排名做加权 |
| 球员效率 | 拦截+抢断 ÷ 出场时间 |
完整一键运行示例
import pandas as pd
import matplotlib.pyplot as plt
data = {
'球队': ['A队']*5 + ['B队']*5,
'球员': ['张三','李四','王五','赵六','钱七',
'孙八','周九','吴十','郑一','冯二'],
'拦截': [3,5,2,4,1, 4,6,3,2,5],
'抢断': [2,4,3,1,5, 3,2,4,6,1]
}
df = pd.DataFrame(data)
# 汇总
summary = df.groupby('球队')[['拦截','抢断']].agg(['sum','mean'])
print(summary)
# 可视化
df.groupby('球队')[['拦截','抢断']].sum().plot(kind='bar', figsize=(7,4))'两队拦截抢断对比')
plt.xticks(rotation=0)
plt.tight_layout()
plt.show()
如果你有具体的数据源(如 Excel、SQL、某个 API),可以告诉我格式,我帮你写对应的读取和分析代码。 你目前的数据是来自:
- 📄 本地 CSV/Excel?
- 🌐 某个网站/API?
- ✍️ 自己手动录入?