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

Python中计算预期分数差值及基于首次基准的期望变化分析

计算人员期望分数变化及年末表现概览

步骤1:数据预处理

首先需要把Exp_Score从字符串转为数值类型,同时将Date转为日期格式,方便后续分组和排序操作:

import pandas as pd

# 原始数据
df1 = pd.DataFrame({"Person": ["James", "Linn", "James", "Linn"],
                    "Date": ["2022-01-01", "2022-01-01", "2022-01-08", "2022-01-08"],
                    "Exp_Score": ["20", "18", "21", "17"]})

# 转换数据类型
df1["Exp_Score"] = df1["Exp_Score"].astype(int)
df1["Date"] = pd.to_datetime(df1["Date"])

步骤2:提取基准分数并计算差值

按Person分组,提取每个人首次记录的分数作为基准值,再合并回原表计算分数差值:

# 获取每个人的基准分数(首次日期的分数)
base_scores = df1.groupby("Person")["Exp_Score"].first().reset_index()
base_scores.rename(columns={"Exp_Score": "Base_Score"}, inplace=True)

# 合并基准分数到原表
df1 = df1.merge(base_scores, on="Person", how="left")

# 计算当前分数与基准分数的差值
df1["Score_Diff"] = df1["Exp_Score"] - df1["Base_Score"]

处理后的DataFrame结果:

Person       Date  Exp_Score  Base_Score  Score_Diff
0   James 2022-01-01         20          20           0
1    Linn 2022-01-01         18          18           0
2   James 2022-01-08         21          20           1
3    Linn 2022-01-08         17          18          -1

步骤3:生成年末表现概览

取每个人最后一次记录的差值,判断其最终表现:

# 按日期排序后,取每个人的最后一条记录
final_records = df1.sort_values("Date").groupby("Person").last().reset_index()

# 根据差值判断表现
final_records["Performance"] = final_records["Score_Diff"].apply(
    lambda x: "超预期" if x > 0 else "未达预期" if x < 0 else "与预期持平"
)

# 提取概览结果
performance_overview = final_records[["Person", "Base_Score", "Exp_Score", "Score_Diff", "Performance"]]

最终概览表:

Person  Base_Score  Exp_Score  Score_Diff Performance
0   James          20         21           1        超预期
1    Linn          18         17          -1      未达预期

如果数据包含更多日期,这段代码会自动取最新的记录来判断年末表现。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 15:25:14