1

我有一个熊猫数据框

from pandas import DataFrame, Series

其中每一行对应一个案例,每一列对应一个月。我想在每 12 个月期间执行滚动总和。看起来很简单,但我被卡住了

result = [x for x.rolling_sum(12) in df.iterrows()]
result = [x for x.rolling_sum(12) in df.T.iteritems()]    

SyntaxError:无法分配给函数调用

a = []
for x in df.iterrows():
    s = x.rolling_sum(12)
    a.append(s)

AttributeError:“元组”对象没有属性“rolling_sum”

4

1 回答 1

3

我想也许你正在寻找的是

pd.rolling_sum(df, 12, axis=1)

在这种情况下,不需要列表理解。该参数使Pandasaxis=1计算.df

例如,

import numpy as np
import pandas as pd
ncols, nrows = 13, 2
df = pd.DataFrame(np.arange(ncols*nrows).reshape(nrows, ncols))
print(df)
#    0   1   2   3   4   5   6   7   8   9   10  11  12
# 0   0   1   2   3   4   5   6   7   8   9  10  11  12
# 1  13  14  15  16  17  18  19  20  21  22  23  24  25

print(pd.rolling_sum(df, 12, axis=1))

印刷

   0   1   2   3   4   5   6   7   8   9   10   11   12
0 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN   66   78
1 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN  222  234

关于您的列表理解:

您以错误的顺序获得了列表理解的部分。尝试:

result = [expression for x in df.iterrows()]

有关列表推导的更多信息,请参阅文档

列表推导的基本形式是

[expression for variable in sequence]

结果列表等价于resultPython 执行后:

result = []
for variable in sequence:
    result.append(expression)

有关列表推导的完整语法,请参阅此链接。

于 2013-08-12T01:53:11.207 回答