Pandas Dataframe:查找满足每行不同条件的所有观测值的条件均值

时间:2018-05-14 17:24:43

标签: python pandas dataframe conditional mean

我们假设我有一个这样的数据框:

            date   M1_start     M1_end  SimPrices_t0_exp
    0 2017-12-31 2018-01-01 2018-01-31         16.151667
    1 2018-01-01 2018-02-01 2018-02-28         45.138445
    2 2018-01-02 2018-02-01 2018-02-28         56.442648
    3 2018-01-03 2018-02-01 2018-02-28         59.769931
    4 2018-01-04 2018-02-01 2018-02-28         50.171695

我想得到SimPrices_t0_exp观察值的平均值,其值为' date'每次观察都在M1_start和M1_end之间

我试过这个

    mask = ((df['date'] >= df['M1_start']) & (df['date'] <= df['M1_end']))
    df['mymean'] = df['SimPrices_t0_exp'][mask].mean()

如果每次观察都会返回NaN,我相信因为每个行都会应用蒙版,分别检查其自身日期的掩码条件,该日期永远不会返回true。

有人能帮助我吗?我这两天一直在努力解决这个问题

示例:对于第一次观察,得到的列在其第一次观察时将在此特定情况下平均为45.13,56.44,59.76,50.17

如果它对某人有帮助,那么伪代码将是这样的:

for obs in observations:
   start = obs.start
   end = obs.end
   sum = 0
   obs_count = 0
   for obs2 in observations:
      if obs2.date >= start and obs2.date <= end:
         sum += obs.SimPrices_t0_exp
         obs_count += 1
   obs.mean = sum/obs_count

谢谢!

1 个答案:

答案 0 :(得分:0)

这里,一种方法是使用笛卡尔合并(不是大数据集的好选择),过滤和groupby

df = df.assign(key=1)
df_m = df.merge(df, on='key')

df_m.query('M1_start_x <= date_y <= M1_end_x').groupby(['M1_start_x','M1_end_x'])['SimPrices_t0_exp_y'].mean()

输出:

M1_start_x  M1_end_x  
2018-01-01  2018-01-31    52.88068
Name: SimPrices_t0_exp_y, dtype: float64