Python 如何通过考虑当前日期和以前的所有日期数据来获得每个日期的平均值
我有下面的数据框,我想通过考虑当前日期和以前所有日期的数据来计算每个日期的累积平均值Python 如何通过考虑当前日期和以前的所有日期数据来获得每个日期的平均值,python,python-3.x,pandas,dataframe,pandas-groupby,Python,Python 3.x,Pandas,Dataframe,Pandas Groupby,我有下面的数据框,我想通过考虑当前日期和以前所有日期的数据来计算每个日期的累积平均值 df = pd.DataFrame({'Items':['Item1', 'Item2', 'Item1', 'Item2', 'Item1', 'Item2', 'Item1', 'Item2', 'Item1', 'Item2', 'Item1'], 'Variable': ['V1', 'V2', 'V1', 'V2', 'V1', 'V2', 'V1', 'V2', 'V1', 'V2
df = pd.DataFrame({'Items':['Item1', 'Item2', 'Item1', 'Item2', 'Item1', 'Item2', 'Item1', 'Item2', 'Item1', 'Item2', 'Item1'],
'Variable': ['V1', 'V2', 'V1', 'V2', 'V1', 'V2', 'V1', 'V2', 'V1', 'V2', 'V1'],
'Date': ['2020-12-16', '2020-12-16', '2020-12-16', '2020-12-16', '2020-12-17', '2020-12-17', '2020-12-17', '2020-12-17', '2020-12-18', '2020-12-18', '2020-12-18'],
'Value': [5, 2, 5, 1, 1, 1, 1, 2, 1, 1, 1]})
df = df.sort_values(['Date'], ascending=[True])
但下面的脚本没有帮助:
df.groupby(['Items', 'Variable', 'Date'])['Value'].expanding().mean().reset_index(name='Value')
我需要的结果如下:
在MS excel中,我们可以通过选择前面的所有行(如下所示)找到最近日期-2018年的平均值:
如上所述,我想计算所有日期试试:
df = df.sort_values(['Items', 'Date', 'Variable'], ascending=[True, True, True])
x = df.reset_index().groupby(['Items', 'Variable'])['Value']
index = x.cumcount()+1
df['Value'] = x.cumsum()/(index.values)
df1 = df[np.where(df[['Items', 'Variable', 'Date']].duplicated(keep='last'), False, True)].reset_index(drop=True)
df1:
编辑:
而不是df[np.wheredf[['Items','Variable','Date']]。duplicatedkeep='last',False,True]。reset\u indexdrop=True
使用df.drop\u duplicatesubset=['Items','Variable','Date',keep='last'。reset\u indexdrop=True尝试:
df = df.sort_values(['Items', 'Date', 'Variable'], ascending=[True, True, True])
x = df.reset_index().groupby(['Items', 'Variable'])['Value']
index = x.cumcount()+1
df['Value'] = x.cumsum()/(index.values)
df1 = df[np.where(df[['Items', 'Variable', 'Date']].duplicated(keep='last'), False, True)].reset_index(drop=True)
df1:
编辑:
而不是df[np.wheredf[['Items','Variable','Date']]。duplicatedkeep='last',False,True]。reset\u indexdrop=True
使用df.drop\u duplicatesubset=['Items','Variable','Date',keep='last'。重置\u indexdrop=True从您的excel工作表中我可以看到,这是您想要做的:
df = df.sort_values(['Variable','Date'], ascending=[True,True])
df['cummean'] = df.groupby(['Variable'])['Value'].transform(lambda x: x.rolling(6,1).mean())
哪个回报
Items Variable Date Value cummean
0 Item1 V1 2020-12-16 5 5.000000
2 Item1 V1 2020-12-16 1 3.000000
4 Item1 V1 2020-12-17 1 2.333333
6 Item1 V1 2020-12-17 1 2.000000
8 Item1 V1 2020-12-18 1 1.800000
10 Item1 V1 2020-12-18 1 1.666667
1 Item2 V2 2020-12-16 5 5.000000
3 Item2 V2 2020-12-16 1 3.000000
5 Item2 V2 2020-12-17 1 2.333333
7 Item2 V2 2020-12-17 2 2.250000
9 Item2 V2 2020-12-18 1 2.000000
从您的excel工作表中我可以看到,这是您想要做的:
df = df.sort_values(['Variable','Date'], ascending=[True,True])
df['cummean'] = df.groupby(['Variable'])['Value'].transform(lambda x: x.rolling(6,1).mean())
哪个回报
Items Variable Date Value cummean
0 Item1 V1 2020-12-16 5 5.000000
2 Item1 V1 2020-12-16 1 3.000000
4 Item1 V1 2020-12-17 1 2.333333
6 Item1 V1 2020-12-17 1 2.000000
8 Item1 V1 2020-12-18 1 1.800000
10 Item1 V1 2020-12-18 1 1.666667
1 Item2 V2 2020-12-16 5 5.000000
3 Item2 V2 2020-12-16 1 3.000000
5 Item2 V2 2020-12-17 1 2.333333
7 Item2 V2 2020-12-17 2 2.250000
9 Item2 V2 2020-12-18 1 2.000000
但它的给予是重复的,但它的给予duplicates@Pygirl谢谢。这是有效的。提前祝你圣诞快乐,新年快乐。谢谢:@Pygirl使用cumsum和cumcount+1的主意不错。我想你可以用drop\u duplicates代替np.where+duplicated,也不需要指定升序参数,它的默认值为True。@Pygirl如果你愿意,我可以编辑答案:我更新了我的asnwer。我本来想用drop_duplicate,但没法写syntax你的评论让我清楚地知道如何使用它np.where+duplicated->drop_duplicate。谢谢@ShubhamSharma@Pygirl快乐编码:@Pygirl非常感谢。这是有效的。提前祝你圣诞快乐,新年快乐。谢谢:@Pygirl使用cumsum和cumcount+1的主意不错。我想你可以用drop\u duplicates代替np.where+duplicated,也不需要指定升序参数,它的默认值为True。@Pygirl如果你愿意,我可以编辑答案:我更新了我的asnwer。我本来想用drop_duplicate,但没法写syntax你的评论让我清楚地知道如何使用它np.where+duplicated->drop_duplicate。谢谢@ShubhamSharma@Pygirl快乐编码: