Reputation: 169
I have two Dataframes:
value_df = pd.DataFrame({'value' : [10,20,30,40,50,60]})
iter_df = pd.DataFrame({'index_of_value' : [1,1,1,2,2,2], 'sec_value' : [1,2,3,4,5,6]})
I need to transform dataframe by view. I have solution for task where i group by 'index_of_value':
def write(group):
seq_list = group.sec_value.tolist()
index = np.unique(group.index_of_value).astype(int)
seq_list.append(value_df.value.iloc[index[0]])
seq_list = ','.join(str(e) for e in seq_list)
return seq_list
result = pd.DataFrame(columns=['seq'])
result['seq'] = iter_df.groupby(['index_of_value']).apply(write)
result = result.reset_index()
But i need instead of groupby split dataframe on N-rows. For example we start from index = 0 with window = 10 and next iteration must start from index = 1 not from 10.
Output for window size = 3
index_of_value seq
0 1 1,2,3,20
1 2 2,3,4,20
2 1 3,4,5,20
3 2 4,5,6,30
Upvotes: 1
Views: 641
Reputation: 10779
I use this little function to iterate over a dataframe in windows.
nrows
is the window size. You can set the overlap
if you want overlapping windows.
def split_df_with_overlap(df, nrows, overlap=0):
for i in range(0, len(df) - overlap, nrows - overlap):
yield df.iloc[i : i + nrows]
Upvotes: 2