2 回答
TA贡献1900条经验 获得超5个赞
据我了解-每当您Timestep达到 0时,您都需要一个新的 DataFrame-
这是你可以尝试的
#This will give you the location of all zeros [0, 3, 7]
zero_indices = list(df.loc[df.Timestep == 0].index)
#We append the number of rows to this to get the last dataframe [0, 3, 7, 9]
zero_indices.append(len(df))
#Then we get the ranges - tuples of consecutive entries in the above list [(0, 3), (3, 7), (7, 9)]
zero_ranges = [(zero_indices[i], zero_indices[i+1]) for i in range(len(zero_indices) - 1)]
#And then we extract the dataframes into a list
list_of_dfs = [df.loc[x[0]:x[1] - 1].copy(deep=True) for x in zero_ranges]
TA贡献1810条经验 获得超4个赞
现在在移动设备上无法对此进行测试,但您可以通过以下方式完成:
current_sequence_index = -1
sequences = []
for __, row in data.iterrows():
if row.Timestep == 0:
sequences.append(pd.DataFrame())
current_sequence_index += 1
sequences[current_sequence_index].append(row, ignore_index=True)
本质上,这将遍历您的数据并在 Timestep 为 0 时生成一个新的 DataFrame。此解决方案有一些假设:1. Timestep 的开始始终为 0。 2. Timesteps 始终是顺序的。
添加回答
举报
