Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas iloc complex slice every nth row

I have a dataframe with a periodicity in the rows of 14 i.e. there are 14 lines of data per record (means, sdev etc.) and I want to extract the 2nd, 4th, 7th and 9th line, repeatedly for every record (14 lines). My code is:

Mean_df = df.iloc[[1,3,6,8]::14,:].copy()

which does not work

TypeError: cannot do slice indexing on <class 'pandas.core.indexes.range.RangeIndex'> with these indexers [[1, 3, 6, 8]] of <class 'list'>

I got help with the code from here, which has been useful, but not on the multi-row selections -- Pandas every nth row

I can extract as several different slices and combine, but it feels like there may be a more elegant solution.

Any ideas?

like image 282
BAC83 Avatar asked Oct 20 '25 02:10

BAC83


2 Answers

Using:

df[np.isin(np.arange(len(df))%14,np.array([1,3,6,8]))]
like image 166
BENY Avatar answered Oct 22 '25 05:10

BENY


You can use a tuple comprehension with slice and np.r_:

arr = np.arange(14*3)
slices = tuple(slice(i, len(arr), 14) for i in (1, 3, 6, 8))

res = np.r_[slices]

print(res)

array([ 1, 15, 29,  3, 17, 31,  6, 20, 34,  8, 22, 36])

In this example, indexing dataframe rows with 1::14 is equivalent to indexing with slice(1, df.shape[0], 14).

This is fairly generic, you can define any tuple of slice objects and pass to np.r_.

like image 23
jpp Avatar answered Oct 22 '25 05:10

jpp