python - 熊猫数据框,对滚动窗口中的任何列进行计算
问题描述
也许 pandas.DataFrame.rolling 不是最好的方法,请告诉我是否有更好的方法。
我想要的是在 df 上有滚动窗口,并在窗口中使用 df 中的所有列来进行各种计算。
我相信下面的代码非常接近我的目标,但我很难理解代码中所述的索引问题。
一开始x.index = RangeIndex(start=0, stop=2, step=1),tmp_df正确选择了df中的第一行和第二行(索引0和1)。对于最后一个 x.index = RangeIndex(start=4, stop=6, step=1) 似乎 iloc 尝试在 df 中选择超出范围的索引 6(df 的索引为 0 到 5)。
我错过了什么?
提前感谢您的任何建议。
import numpy as np
import pandas as pd
df = pd.DataFrame({'open': [7, 5, 10, 11,6,12],
'close': [6, 6, 11, 10,7,10],
'positive': [0, 1, 1, 0,1,0]},
)
def do_calculations_on_any_df_column_in_window(x,df):
print("index:",x.index)
tmp_df = df.iloc[x.index] # raises "ValueError: cannot set using a slice indexer with a different length than the value" when x.index = RangeIndex(start=4, stop=6, step=1) as df index goes from 0 to 5 only
# do calulations on any column in tmp_df, get result
result = 1 #dummyresult
return result
intervals = range(2, 10)
for i in intervals:
df['result_' + str(i)] = np.nan
res = df.rolling(i).apply(do_calculations_on_any_df_column_in_window, args=(df,), raw=False)
df['result_' + str(i)][1:] = res
print(df)
解决方案
试试这个功能:
def calculate_on_rolling_window(df, win, col_names):
#final_df = pd.DataFrame() # stores the complete results
# calculate sd and mean for each tag
for i in range(len(col_names)):
current_column = col_names[i]
df[current_column + '_mean_' +str(win)] = (df[current_column].rolling(window=win).mean())
df[current_column + '_min_' +str(win)] = (df[current_column].rolling(window=win).min())
df[current_column + '_max_' +str(win)] = (df[current_column].rolling(window=win).max())
df = df.fillna(0)
return(df)
你得到这个结果
col_names = df.columns
df_extended = calculate_on_rolling_window(df,2,col_names)
df_extended.head()
open close positive open_mean_2 open_min_2 open_max_2 close_mean_2 close_min_2 close_max_2 positive_mean_2 positive_min_2 positive_max_2
0 7 6 0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1 5 6 1 6.0 5.0 7.0 6.0 6.0 6.0 0.5 0.0 1.0
2 10 11 1 7.5 5.0 10.0 8.5 6.0 11.0 1.0 1.0 1.0
3 11 10 0 10.5 10.0 11.0 10.5 10.0 11.0 0.5 0.0 1.0
4 6 7 1 8.5 6.0 11.0 8.5 7.0 10.0 0.5 0.0 1.0
推荐阅读
- php - 如何在 laravel 中将模型函数调用到我的控制器中
- laravel - 无法访问 hasMany 模型
- r - 如果零值出现在另一列上,则删除带有因子的行
- python - 将数据框转换为字典
- javascript - 我们可以打印/存储 console.time() - console.timeEnd() 的值吗?
- python - 过滤具有 DateRange 字段为“validity_range”的多个模型
- php - 无法在单个产品页面中重新排序产品选项卡?
- rstudio - 绘制大型 POS 数据集的每周平均利润
- javascript - 删除字符串 REGEX 中的正斜杠但不删除日期格式
- batch-file - 在多个子文件夹中用 file.new 替换 file.old