'How to slice the dataset in Python in specific intervals

I have a dataset with n rows, how can I access a specific number of rows every specific number of rows through the whole dataset using Python?

For example, in 100 rows data set and I want to access 10 rows every 10 rows, like 1:10, 20:30, 40:50, 60:70, 80:90



Solution 1:[1]

I could think of something like this

df.iloc[np.array([int(x/10) for x in df.index]) % 2 == 0]

It takes the index of the dataframe, divides it by 10 and casts it to an int. This basically just removes the last digit in this example.

With the modulo statement the first 10 rows are True, the next 10 False and so on. This is then used with iloc to get just the lines with the True value.

This requires a continuously increasing index. If for example some rows were already filtered out this is not the case. reset_index can be used to reset the index.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1