简体   繁体   English

Python / Pandas:用于循环以汇总移动范围并追加到新列

[英]Python/Pandas: For loop to sum moving range and append to new column

I am fairly new to Python and Pandas so please forgive the uninformed question I'm posing here. 我对Python和Pandas相当陌生,所以请原谅我在这里提出的不为人知的问题。

I have a dataframe (QReport) that looks like so: 我有一个数据框(QReport)如下所示:

DATE            STATE       SALES
2010-12-01       AL          10
2010-12-02       AL          13
2010-12-03       AL          16
2010-12-04       AL          10
2010-12-05       AL          10
2010-12-06       AL          2
2010-12-07       AL          5 
2010-12-08       AL          5

I'd like to create a seven day sales total column that sums seven day slices of sales and adds that total to a new '7DTotal' column. 我想创建一个为期7天的销售总额列,该列将7天的销售额汇总并添加到新的“ 7DTotal”列中。 I've been trying to get at this through a for loop, setting x and y variables as 0 (zero) and 6 indexes which I try to increment each run through the loop: 我一直在尝试通过for循环来实现此目的,将x和y变量设置为0(零)和6个索引,我试图通过循环将每次运行递增:

x = 0
y = 6
for i in QReport:
    QReport['7DTotal'] = sum(QReport['SALES'][x:y])
    x = x + 1
    y = y + 1

This approach, however, is not working and is returning the same value for every row in the newly-added 7DTotal column. 但是,这种方法不起作用,并且为新添加的7DTotal列中的每一行返回相同的值。

Expected output would be: 预期输出为:

DATE            STATE       SALES   7DTotal
2010-12-01       AL          10        0
2010-12-02       AL          13        0
2010-12-03       AL          16        0
2010-12-04       AL          10        0
2010-12-05       AL          10        0
2010-12-06       AL          2         0
2010-12-07       AL          5         66
2010-12-08       AL          5         61

Thanks in advance for any help! 在此先感谢您的帮助!

You should use rolling sum: 您应该使用滚动总和:

QReport['7DTotal'] = pd.rolling_sum(QReport.SALES, 7).fillna(0)

or 要么

# Suggested by a commenter
QReport['7DTotal'] = QReport.rolling(7).sum().fillna(0)

(The first six elements of the column will be undefined, and fillna fills them with 0s.) (该列的前六个元素是未定义的, fillna用0填充它们。)

an easy solution would be 一个简单的解决方案是

7_day_list = []

for index in QReport.

    avg = np.mean(QReport.loc[index:index+7, 'SALES'])

    7_day_list.append(avg)

hope that helps 希望能有所帮助

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

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