繁体   English   中英

我如何在 python 中为 n+(n+12)+(n+24)+(n+36) 然后为 (n+1)+(n+13)+(n+25) 等进行求和直到达到 n+12?

[英]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?

因此,假设我有月度数据,并且我正在尝试找到一种月度变化,但我想要的月度变化将是以下具有此数据框的数据框,从 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

我想到达 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)

所以最后我将只有 12 个数字作为 output。 我一直在尝试使用def来执行此操作并定义 function 但是在进入这部分时我根本不知道该怎么做。

实际上,我对使用 python/pandas 管理数据帧非常陌生,因此我将不胜感激。

假设 'Month Date' 是一个字符串,按季度分组(由.str[:1]提取)和总和:

df['Value'].groupby(df['Month Date'].str[:1]).sum()

如果第一部分是一个月(可以是两位数):

df['Value'].groupby(df['Month Date'].str.split('-').str.get(0)).sum()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM