Customization
import pandas as pd import numpy as np np.random.seed([3,1415]) df = pd.DataFrame(np.random.rand(20, 2), columns=['A', 'B'])
You need to create shortcuts for grouping yourself. I would use:
(df.index.to_series() / 5).astype(int)
To get a series of values, such as [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, ...] Then use this in groupby
You will also need to specify an index for the new framework. I would use:
df.index[4::5]
To get the current index, starting at 5th position (hence 4 ) and every fifth position after that. It will look like [4, 9, 14, 19] . I could do this as df.index[::5] to get the starting positions, but I went with the ending positions.
Decision
It looks like:
AB 4 0.198019 0.320451 9 0.329750 0.408232 14 0.293297 0.223991 19 0.095633 0.376390
Other considerations
This is equivalent to fetching down. We did not consider the sample.
To get back to what we did with the dataframe index something more frequent, we can use reindex as follows:
It looks like:
AB 0 0.198019 0.320451 1 0.198019 0.320451 2 0.198019 0.320451 3 0.198019 0.320451 4 0.198019 0.320451 5 0.329750 0.408232 6 0.329750 0.408232 7 0.329750 0.408232 8 0.329750 0.408232 9 0.329750 0.408232 10 0.293297 0.223991 11 0.293297 0.223991 12 0.293297 0.223991 13 0.293297 0.223991 14 0.293297 0.223991 15 0.095633 0.376390 16 0.095633 0.376390 17 0.095633 0.376390 18 0.095633 0.376390 19 0.095633 0.376390
We could also use other things for reindex , for example, range(0, 20, 2) , to get samples to even indices.