Python 数据帧列表:将数据帧切片为数据帧列表

Python 数据帧列表:将数据帧切片为数据帧列表,python,pandas,Python,Pandas,我有下面的函数,它应该返回一个数据帧列表。这些数据帧不得包含任何已包含的值 idx是满足条件的索引列表(dummy=1)。 然后丢弃假人(n)周围的所有物体 我的输出应该是一个数据帧列表,其中包含未删除的值,但不包含其他值(在两个假人之间)。第一个数据帧是正常的。我对元素进行计数,并使用for循环尝试收集其他切片,但是,切片不会返回在所需限制内的数据帧 data = pd.DataFrame(data={"A":[1,2,3,4,5,6,7,8,9,10],

我有下面的函数,它应该返回一个数据帧列表。这些数据帧不得包含任何已包含的值

idx是满足条件的索引列表(dummy=1)。 然后丢弃假人(n)周围的所有物体

我的输出应该是一个数据帧列表,其中包含未删除的值,但不包含其他值(在两个假人之间)。第一个数据帧是正常的。我对元素进行计数,并使用for循环尝试收集其他切片,但是,切片不会返回在所需限制内的数据帧

data = pd.DataFrame(data={"A":[1,2,3,4,5,6,7,8,9,10], 
                          "B":[1,3,3,4,5,6,7,8,9,10],
                      "event":[0,0,0,0,1,0,0,0,1,0]})

def EstimationWindow (data, n=3, dummy=1):
    '''
    data....data. Contains ALL data - reurns, and event dummies = event column
    dummy...event=1
    n.......days before/after
    '''    
    idx = data.index.get_indexer_for(data[data.event==dummy].index)
    # Drop event window
    estwin = data.drop((np.unique(np.concatenate([np.arange(max(i-n,0), min(i+n+1, len(data))) for i in idx]))))    
#    estwin = [estwin.iloc[0:i-n] for i in idx]
    output = [estwin.iloc[0:idx[0]-n]]
    for i in idx[1:]:
        out = pd.DataFrame(estwin.loc[len(output):i-n])
        output.append(out)
    return(output)
函数应该返回一个列表:
output=[df1,df2]

通缉:

[   A  B  event
 0  1  1      0
 1  2  3      0
 2  3  3      0,    A  B  event
 6  7  7      0]
结果:

 [   A  B  event
 0  1  1      0
 1  2  3      0
 2  3  3      0,    A  B  event
 1  2  3      0
 2  3  3      0
 6  7  7      0]

无需使用
for
循环来构建拆分
df
s的列表。找到虚拟对象,用于构建要删除的索引,然后直接使用:

然后

屈服

   A  B  event
0  1  1      0
1  2  3      0
2  3  3      0

   A  B  event
6  7  7      0

作品非常感谢你!
for _, g in df.drop(ind_to_drop).groupby(c):
    print(g)
   A  B  event
0  1  1      0
1  2  3      0
2  3  3      0

   A  B  event
6  7  7      0