2 回答

TA貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個(gè)贊
據(jù)我了解-每當(dāng)您Timestep達(dá)到 0時(shí),您都需要一個(gè)新的 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貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊
現(xiàn)在在移動(dòng)設(shè)備上無法對此進(jìn)行測試,但您可以通過以下方式完成:
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)
本質(zhì)上,這將遍歷您的數(shù)據(jù)并在 Timestep 為 0 時(shí)生成一個(gè)新的 DataFrame。此解決方案有一些假設(shè):1. Timestep 的開始始終為 0。 2. Timesteps 始終是順序的。
添加回答
舉報(bào)