[英]How do I do a sum in python for n+(n+12)+(n+24)+(n+36) and then for (n+1)+(n+13)+(n+25) and so on until reaching n+12?
So lets say I have monthly data and I am trying to find a type of monthly change but the monthly change I want would be the following having this data frame the one I have is much bigger having every month from 2010 to 2019.因此,假设我有月度数据,并且我正在尝试找到一种月度变化,但我想要的月度变化将是以下具有此数据框的数据框,从 2010 年到 2019 年,我每个月拥有的数据框要大得多。
axis Month Date Value
1 1-2012 10
2 2-2012 11
3 3-2012 15
4 1-2013 12
5 2-2013 13
6 3-2013 17
7 1-2014 15
8 2-2014 16
9 3-2014 20
I want to arrive to an output such as我想到达 output 例如
axis value_sum
1. 37
2. 40
3. 52
1.which is equal as the sum of axis(1+4+7) 2.which is equal as the sum of axis(2+5+8) 3.which is equal as the sum of axis(3+6+9)
so at the end I will have just 12 numbers as an output.所以最后我将只有 12 个数字作为 output。 Iv been trying to do this as with
def
and defining a function but when getting to this part I simply dont know what to do.我一直在尝试使用
def
来执行此操作并定义 function 但是在进入这部分时我根本不知道该怎么做。
I actually am pretty new with managing data frames with python/pandas so I would appreciate the help.实际上,我对使用 python/pandas 管理数据帧非常陌生,因此我将不胜感激。
Assuming 'Month Date' is a string, group by quarter (extracted by .str[:1]
) and sum:假设 'Month Date' 是一个字符串,按季度分组(由
.str[:1]
提取)和总和:
df['Value'].groupby(df['Month Date'].str[:1]).sum()
If first part is a month (can be two digit):如果第一部分是一个月(可以是两位数):
df['Value'].groupby(df['Month Date'].str.split('-').str.get(0)).sum()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.