mahmood
mahmood

Reputation: 24705

Iterating rows of dataframe by steps

In the following code, I want to iterate over a dataframe by 14 steps.

for index, row in df.iterrows():
    print(index)
    batch_df = pd.DataFrame( df[index:index+14] )
    index = index + 14

The printed index numbers are sequential starting from 0,1,... However I expect to see 0,14,28,...

How can I fix that?

Upvotes: 0

Views: 1581

Answers (2)

Tobias P. G.
Tobias P. G.

Reputation: 820

From what context you provided I don't see why you need to loop through the dataframe. I think using range(start, end, step) will give you your desired result.

for i in range(0, len(df), 14):
    batch_df = df.iloc[i:i+14]

Upvotes: 1

Joshua Voskamp
Joshua Voskamp

Reputation: 2054

To translate Umar.H's comment to match your code:

for _, batch_df in df.groupby(df.index//14):
    <your code>

BUT as Umar.H mentioned, whatever you're wanting to accomplish can likely (and should, if feasible) be accomplished without the use of a for-loop at all. pandas has a host of useful functions that may already do what you need; and even if not, a .apply or .transform allow you to use your own custom functions. For example:

df['new_metric'] = df.groupby(df.index//14).transform(custom_func)

Upvotes: 0

Related Questions