具有动态日期的条件分组平均数



我有一个数据帧,我需要计算动态开始日期和结束日期之间的groupby(class_1&class_2(平均值。

结束日期为日期前1天,开始日期为结束日期前1年。有可能得到(示例(的平均值吗:class_1:math,class_2:math其中开始日期和结束日期之间的位置为1。

样品df

+----------+---------+---------+-----------+----------+----------+|日期|class_1|class_2|placement|end |1年|+----------+---------+---------+-----------+----------+----------+|21年5月12日|数学|数学|1 | 21年4月12日|20年4月||21年4月12日|数学|eng|3|21年3月12日|20年3月||21年3月12日|eng|math|4|21年2月12日|12年2月||21年2月12日|数学|数学|4 | 21年1月12日|20年1月||21年1月12日|数学|数学|1 | 21年11月30日| 20年11月3日||2021年11月30日|数学|数学| 2 | 21年11月29日| 20年11月9日|+----------+---------+---------+-----------+----------+----------+

样本输出

+----------+---------+---------+-----------+----------+----------+--------+|日期|class_1|class_2|位置|end |1yr|平均值|+----------+---------+---------+-----------+----------+----------+--------+|21年5月12日|数学|数学|1 | 21年4月12日|20年4月2日|0.3333||21年4月12日|数学|eng|3|21年3月12日|20年3月3日|0||21年3月12日|eng|math|4|21年2月12日|12年2月20日|0||21年2月12日|数学|数学|4 |21年1月12日|20年1月1日|0.5||21年1月12日|数学|数学|1 | 21年11月30日| 20年11月3日|0||21年11月30日|数学|数学|2 | 21年11日29日| 20年11月29日|0|+----------+---------+---------+-----------+----------+----------+--------+

为了简单起见,我在上面的示例中使用了11/30/21到12/05/21。

我对静态日期使用了set_index((、groupby((、assign((和reset_index((方法,但我不知道如何将它们应用于动态日期。

提前感谢

您可以使用apply逐行计算您的分数:

def compute_1yr_mean(row):
# Get the rows belonging to the period and classes of interest
curr_df = df[
# select row in the time window computed starting from the current row
(df['date'] >= row['1yr'])
# or you can compute this date on the fly, as shown in the next line
# (df['date'] >= row['date'] - datetime.timedelta(days=1) - relativedelta(years=1))
& (df['date'] < row['date'])
# select same classes
& (df['Class_1'] == row['Class_1'])
& (df['Class_2'] == row['Class_2'])
]
# Compute the ratio between the number of rows with placement 1 and the number of all rows
return (
(curr_df['placement'] == 1).sum() / len(curr_df)
) if len(curr_df) else 0
df['score_mean_1yr'] = df.apply(compute_1yr_mean, axis=1)

假设您感兴趣的数据帧是:

date Class_1 Class_2  placement        end        1yr
0 2021-12-05    math    math          1 2021-12-04 2020-12-04
1 2021-12-04    math     eng          3 2021-12-03 2020-12-03
2 2021-12-03     eng    math          4 2021-12-02 2020-12-02
3 2021-12-02    math    math          4 2021-12-01 2020-12-01
4 2021-12-01    math    math          1 2021-11-30 2020-11-30
5 2021-11-30    math    math          2 2021-11-29 2020-11-29

将其应用于数据帧的结果是:

date Class_1 Class_2  placement        end        1yr  score_mean_1yr
0 2021-12-05    math    math          1 2021-12-04 2020-12-04        0.333333
1 2021-12-04    math     eng          3 2021-12-03 2020-12-03        0.000000
2 2021-12-03     eng    math          4 2021-12-02 2020-12-02        0.000000
3 2021-12-02    math    math          4 2021-12-01 2020-12-01        0.500000
4 2021-12-01    math    math          1 2021-11-30 2020-11-30        0.000000
5 2021-11-30    math    math          2 2021-11-29 2020-11-29        0.000000

注意:函数compute_1yr_mean假定df在其定义的范围内存在。

最新更新