Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Dataframe使用窗口创建列表列的滚动聚合_Python_List_Dataframe_Window_Rolling Computation - Fatal编程技术网

Python Dataframe使用窗口创建列表列的滚动聚合

Python Dataframe使用窗口创建列表列的滚动聚合,python,list,dataframe,window,rolling-computation,Python,List,Dataframe,Window,Rolling Computation,我有一个df,它有一列列表 我想知道是否有一种方法可以为给定窗口创建“single_input_vector”列的滚动聚合。我查看了下面的SO链接,但它没有提供包含窗口的方法。在我的例子中,3个窗口所需的输出列是: Row1: [[24.68, 164.93]] Row2: [[24.68, 164.93], [24.18, 164.89]] Row3: [[24.68, 164.93], [24.18, 164.89], [23.99, 164.63]] Row4: [[24.18, 1

我有一个df,它有一列列表

我想知道是否有一种方法可以为给定窗口创建“single_input_vector”列的滚动聚合。我查看了下面的SO链接,但它没有提供包含窗口的方法。在我的例子中,3个窗口所需的输出列是:

Row1: [[24.68, 164.93]] 
Row2: [[24.68, 164.93], [24.18, 164.89]]
Row3: [[24.68, 164.93], [24.18, 164.89], [23.99, 164.63]] 
Row4: [[24.18, 164.89], [23.99, 164.63], [24.14, 163.92]]

诸如此类。

我想不出更有效的方法来实现这一点,因此,虽然这确实有效,但在海量数据集上可能存在性能限制

我们基本上使用滚动计数来创建一组开始:停止的切片索引

import pandas as pd
import numpy as np
# Get some time series data
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/timeseries.csv")
input_cols = ['A', 'B']
df['single_input_vector'] = df[input_cols].apply(tuple, axis=1).apply(list)


window = 3

df['len'] = df['A'].rolling(window=window).count()

df['vector_list'] = df.apply(lambda x: df['single_input_vector'][max(0,x.name-(window-1)):int(x.name)+1].values, axis=1)
import pandas as pd
import numpy as np
# Get some time series data
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/timeseries.csv")
input_cols = ['A', 'B']
df['single_input_vector'] = df[input_cols].apply(tuple, axis=1).apply(list)


window = 3

df['len'] = df['A'].rolling(window=window).count()

df['vector_list'] = df.apply(lambda x: df['single_input_vector'][max(0,x.name-(window-1)):int(x.name)+1].values, axis=1)