簡體   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