Home > Net >  Iterating rows of dataframe by steps
Iterating rows of dataframe by steps

Time:11-06

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?

CodePudding user response:

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]

CodePudding user response:

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)
  • Related