python - 如何以逻辑方式收集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。任何帮助将不胜感激!
解决方案
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.
推荐阅读
- javascript - 使用捕获的匹配作为键,用正则表达式替换字符串
- elasticsearch - 启动 ELASTICSEARCH 时,Elasticsearch 进程的最大文件描述符 [4096] 太低
- docker - Docker 应用程序中的 GUI
- c++ - 是否可以在 C++ 中转到更高级别的范围条件?
- amazon-web-services - AWS 简单电子邮件服务适用于本地主机,但不适用于生产 Amazon EC2 服务器
- php - 尝试获取按钮的值时获取 NULL
- javascript - 从 ReactJS 中的组件列表中删除一个组件
- php - PHP 表对齐在 foreach 循环中不正确
- html - 保存数据时如何生成pdf?
- python - 如何在 LDA 中查看每个主题的所有文档?