How to filter every nth row in a dataframe?

I am looking for assistance with filtering every nth row in a pandas DataFrame. Currently, I am using iloc to slice the DataFrame, but I am seeking alternative methods or more efficient ways to achieve this. I would appreciate any suggestions or insights that can help me better understand and implement them.

The ::5 in the iloc function selects every 5th row, starting from the first row (index position 0).

There is another method for selecting rows in a Pandas dataframe that involves using the mod (%) operator. This method can be used to select rows based on their position (index) in the dataframe. It’s particularly useful if you want to select every nth row but don’t know the index positions.

For example, if you want to select every 4th row in a dataframe, you can use the following code:

In this code, the df.index % 4 == 0 condition selects rows where the index position is divisible by 4, which means every 4th row will be selected.