[英]Calculate average of every 7 instances in a dataframe column
I have this pandas dataframe with daily asset prices: Picture of head of Dataframe 我有这个带有每日资产价格的pandas数据框 : Dataframe负责人的图片
I would like to create a pandas series (It could also be an additional column in the dataframe or some other datastructure) with the weakly average asset prices. 我想创建一个熊猫系列(它也可能是数据框或其他数据结构中的附加列),其平均资产价格较低。 This means I need to calculate the average on every 7 consecutive instances in the column and save it into a series.
这意味着我需要计算该列中每7个连续实例的平均值并将其保存为系列。
Picture of how result should look like 结果的外观图
As I am a complete newbie to python (and programming in general, for that matter), I really have no idea how to start. 由于我是python的新手(因此通常是编程),我真的不知道如何开始。
I am very grateful for every tipp! 我非常感谢每一个小费!
I believe need GroupBy.transform
by modulo of numpy array create by numpy.arange
for general solution also working with all indexes (eg with DatetimeIndex
): 我认为需要
GroupBy.transform
通过numpy的阵列的模通过创建numpy.arange
一般溶液(如与还与所有索引工作DatetimeIndex
):
np.random.seed(2018)
rng = pd.date_range('2018-04-19', periods=20)
df = pd.DataFrame({'Date': rng[::-1],
'ClosingPrice': np.random.randint(4, size=20)})
#print (df)
df['weekly'] = df['ClosingPrice'].groupby(np.arange(len(df)) // 7).transform('mean')
print (df)
ClosingPrice Date weekly
0 2 2018-05-08 1.142857
1 2 2018-05-07 1.142857
2 2 2018-05-06 1.142857
3 1 2018-05-05 1.142857
4 1 2018-05-04 1.142857
5 0 2018-05-03 1.142857
6 0 2018-05-02 1.142857
7 2 2018-05-01 2.285714
8 1 2018-04-30 2.285714
9 1 2018-04-29 2.285714
10 3 2018-04-28 2.285714
11 3 2018-04-27 2.285714
12 3 2018-04-26 2.285714
13 3 2018-04-25 2.285714
14 1 2018-04-24 1.666667
15 0 2018-04-23 1.666667
16 3 2018-04-22 1.666667
17 2 2018-04-21 1.666667
18 2 2018-04-20 1.666667
19 2 2018-04-19 1.666667
Detail : 详细说明 :
print (np.arange(len(df)) // 7)
[0 0 0 0 0 0 0 1 1 1 1 1 1 1 2 2 2 2 2 2]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.