简体   繁体   中英

Pandas iloc complex slice every nth row

I have a dataframe with a periodicity in the rows of 14 ie 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?

使用:

df[np.isin(np.arange(len(df))%14,np.array([1,3,6,8]))]

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_ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM