首页 > 解决方案 > 如何以逻辑方式收集for循环输出

问题描述

我在这里可能只是眼睛疲倦,但我不确定我在嵌套 for 循环中搞砸了什么。我正在尝试遍历我们的内部销售数据的年数和数月,以获得我们的销售速度、季节性等的 df(用于最终图表)。出于某种原因,我一直在覆盖第一年的数据列。

几乎所有我能想到的句法调整。我觉得我的眼睛很累...

annual_sales = [] # make empty list for revenue data
annual_sales_df = pd.DataFrame(index = range(1, 13)) # make empty df to collect lists

for i in range(2013, currentYear):
    for j in range(0, 12 + 1):
        annual_sales.append(df.loc[(df['stageName'] == 'Awarded Won') & (df['fiscalYear'] == i) & (df['Month'] == j), 'revenue'].sum()) # collect annual revenue in a list - this part is working correctly, I just left it in for context
    out_df = pd.DataFrame(annual_sales) # put sales list into df
    annual_sales_df = annual_sales_df.merge(annual_out_df, left_index = True, right_index = True, how = 'inner') # merge the intermediate df onto the larger df as columns
print(annual_sales_df) # print the df

我希望在一列中按月获得每年的销售额,2013-2018 年的 6 列数据构成 6 x 12 df。任何帮助将不胜感激!

标签: pythonpandasnested-loops

解决方案


The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.

Syntax:

range(start, stop, step)

Here, one important thing to note is that the stop is exclusive. So, if you want to run a loop from 1 to 12(which is 12 months overall), replace for j in range(0, 12 + 1) with for j in range(1, 13).

Also, there is a wrong dataframe passed inside merge(). There is no annual_out_df dataframe that was created before, but there is out_df. So, I think, it should be out_df in place of annual_out_df in that function.


推荐阅读