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

Python 3.10实现PS5游戏同场次关联游戏及出现次数统计需求

问题描述

我有一组PS5游戏游玩数据(基于Python 3.10开发),数据字段说明如下:

  • session_number:场次编号
  • venue_number:游戏地点
  • game:游玩的游戏
  • start_Ts:场次开始时间
  • end_Ts:场次结束时间

需要生成统计结果,展示每个游戏在同一场次中搭配游玩的其他游戏及其出现次数。例如《Ratchet & Clank》,同场次中《Deathloop》出现2次,《Demon’s Souls》《Godfall》《Miles Morales》《Returnal》各出现1次。

用于生成测试数据的代码如下:

import pandas as pd

# 初始化数据列表
data = [
    [1,'v1','Ratchet & Clank','29/08/2022 13:05:06','29/08/2022 13:54:52',38],
    [1,'v1','Demon’s Souls ','29/08/2022 13:05:06','29/08/2022 13:54:52',87],
    [2,'v1','Deathloop','29/08/2022 13:54:17','29/08/2022 14:06:18',89],
    [2,'v1','Ratchet & Clank','29/08/2022 13:54:17','29/08/2022 14:06:18',48],
    [2,'v1','Miles Morales','29/08/2022 13:54:17','29/08/2022 14:06:18',5],
    [3,'v1','Returnal','29/08/2022 14:11:00','29/08/2022 15:19:10',22],
    [4,'v2','Deathloop','29/08/2022 20:27:32','29/08/2022 20:31:30',58],
    [4,'v2','Returnal','29/08/2022 20:27:32','29/08/2022 20:31:30',97],
    [4,'v2','Ratchet & Clank','29/08/2022 20:27:32','29/08/2022 20:31:30',75],
    [5,'v2','Demon’s Souls ','29/08/2022 20:35:10','29/08/2022 20:36:10',15],
    [6,'v2','Dirt 5 ','29/08/2022 20:40:34','29/08/2022 20:42:04',94],
    [7,'v2','Final Fantasy VII ','29/08/2022 20:45:07','29/08/2022 20:51:04',69],
    [8,'v2','Dirt 5 ','29/08/2022 20:48:45','29/08/2022 20:49:55',85],
    [9,'v3','Devil May Cry 5','29/08/2022 13:12:33','29/08/2022 13:58:57',90],
    [10,'v3','Ratchet & Clank','29/08/2022 14:44:07','29/08/2022 14:51:26',78],
    [10,'v3','Godfall','29/08/2022 14:44:07','29/08/2022 14:51:26',36],
    [11,'v3','Deathloop','29/08/2022 14:48:31','29/08/2022 14:49:58',43]
]

# 创建DataFrame
df = pd.DataFrame(data, columns=['session_number',  'VENUE_NUMBER', 'GAME', 'START_TS', 'END_TS', 'TIME_SEPENT'])
解决方案

通过以下步骤实现需求:

  1. 按场次编号分组,提取每个场次下的所有游戏列表
  2. 生成同场次内的游戏双向配对(A搭配B和B搭配A都要统计)
  3. 对配对结果进行次数统计,整理成清晰的输出格式

实现代码

import pandas as pd

# 按场次分组,获取每个场次的游戏列表
session_game_groups = df.groupby('session_number')['GAME'].apply(lambda x: [g.strip() for g in x]).reset_index()

# 生成所有同场次的游戏配对
game_pairs = []
for _, row in session_game_groups.iterrows():
    current_games = row['GAME']
    # 遍历生成当前场次内的所有双向配对
    for i in range(len(current_games)):
        for j in range(len(current_games)):
            if i != j:
                game_pairs.append((current_games[i], current_games[j]))

# 统计配对出现次数
pair_count_df = pd.DataFrame(game_pairs, columns=['Game', 'Paired_Game'])
result_df = pair_count_df.groupby(['Game', 'Paired_Game']).size().reset_index(name='Count')

# 按游戏名称和次数排序,优化展示效果
result_df = result_df.sort_values(['Game', 'Count'], ascending=[True, False]).reset_index(drop=True)

# 输出结果
print(result_df)

结果说明

运行代码后会得到结构化的统计DataFrame,其中:

  • Game:当前统计的目标游戏
  • Paired_Game:与目标游戏同场次出现的其他游戏
  • Count:该搭配组合出现的场次次数

以《Ratchet & Clank》为例,对应统计结果片段如下:

Game       Paired_Game  Count
0  Ratchet & Clank         Deathloop      2
1  Ratchet & Clank    Demon’s Souls      1
2  Ratchet & Clank           Godfall      1
3  Ratchet & Clank     Miles Morales      1
4  Ratchet & Clank          Returnal      1

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 17:48:29