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

Python中分组、横截面及横截面内排序:获取玩家最新runs_tier

How to Get the Latest runs_tier for Each Player

Got it, let's break down how to solve this problem. You're working with a dataset that tracks player_id, broadcast_month_id, and runs_tier, and you need to extract the most recent runs_tier entry(s) for each of your 500+ players. Since you've already grouped by player_id, the next step is sorting within those groups and picking the top results.

First, let's recap your sample data for context:

player_id broadcast_month_id runs_tier
67        201803             100s
67        201803             400s
67        201802             50s
67        201802             100s
67        201801             50s
...

1. SQL Solution (For Database-Stored Data)

If your data lives in a SQL database, window functions are the cleanest way to rank records and filter for the latest entries.

Query Code:

WITH ranked_player_data AS (
  SELECT
    player_id,
    broadcast_month_id,
    runs_tier,
    -- Rank entries per player, with latest month first
    ROW_NUMBER() OVER (
      PARTITION BY player_id
      ORDER BY broadcast_month_id DESC
    ) AS entry_rank
  FROM your_dataset_name
)
-- Pick only the top-ranked (latest) entries
SELECT
  player_id,
  broadcast_month_id,
  runs_tier
FROM ranked_player_data
WHERE entry_rank = 1;

Quick Notes:

  • PARTITION BY player_id maintains your existing grouping.
  • ORDER BY broadcast_month_id DESC ensures the most recent month (highest numeric value like 201803) comes first.
  • If a player has multiple runs_tier values in the same latest month (like player 67 in 201803), this query returns all of them. If you need just one (e.g., alphabetically sorted), add runs_tier ASC to the ORDER BY clause.

2. Python Pandas Solution (For In-Memory Data)

If you're working with the dataset as a pandas DataFrame, sorting and grouping will get you the results quickly.

Code Example:

import pandas as pd

# Load your data into a DataFrame (adjust this to your actual load method)
df = pd.read_csv("your_data.csv")

# Option 1: Get one latest entry per player (first in sorted order)
# Sort by player, then by month descending
sorted_df = df.sort_values(by=["player_id", "broadcast_month_id"], ascending=[True, False])
latest_single_entry = sorted_df.groupby("player_id").head(1)

# Option 2: Get ALL entries from the latest month per player
# First find the max month for each player
max_months = df.groupby("player_id")["broadcast_month_id"].max().reset_index()
# Merge back to get all entries from that month
latest_all_entries = df.merge(max_months, on=["player_id", "broadcast_month_id"])

Explanation:

  • Option 1 sorts each player's records by month (newest first) and grabs the first row of each group.
  • Option 2 is better if you need to retain all runs_tier values from the most recent month (instead of just one).

Core Idea Across Tools

No matter which method you use, the steps are the same:

  • Keep each player's records grouped together.
  • Sort each group by broadcast_month_id in descending order to prioritize the newest month.
  • Select the top entry(s) from each sorted group.

内容的提问来源于stack exchange,提问作者Shuvayan Das

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:13:41