I would like to perform some action on sliced DataFrame with multiple slice indexes. The pattern is df.iloc[0:24] , df.iloc[24:48], df.iloc[48:72] and so on with step 24 as you get it. How I can iterate it without to set it manually every time. More like df.iloc[x:z] and each iteration x=0, z=24 and next iteration with 24 step, x will be 24 and z=48 and so on. Thanks in advance, Hristo.
Asked
Active
Viewed 8,003 times
5
-
`df.iloc[::24]` This is a _very_ basic indexing problem, you really should look up slice notation. – cs95 Nov 16 '17 at 19:08
-
1No,no. mate I don't have a problem with slicing. But I need specific dataframe slice parts from 0-24 and perform some operations, then df[24:48] and perform some operation with that sliced part so I need to iterate each sliced DF and perform some actions. – Hristo Stoychev Nov 16 '17 at 19:17
-
Ah, alright. Apologies. – cs95 Nov 16 '17 at 19:22
2 Answers
12
for
loop iteration
for i in range(0, len(df), 24):
slc = df.iloc[i : i + 24]
groupby
df.groupby(df.index // 24 * 24).apply(your_function)
-
+ 1 for the for loop, just want to make a note: ‘slc’ is the sliced dataframe that jnclude the iterations, so all operations are performed to the ‘slc’ – n1tk Aug 23 '18 at 08:51
1
output it will give df with
for i in range(0,len(df)):
dfnew = df.iloc[i : i + 42]
i = i + 1
print(dfnew)
0 to 41 indices data 1 to 42 2 to 43 by skipping first entire row with limit 42 rows of data.

Szabolcs Páll
- 1,402
- 6
- 25
- 31

Akshay Salvi
- 199
- 2
- 5
-
Even though this wasn't the solution OP needed, I for sure did, thank you. – NoahVerner Aug 08 '22 at 08:02