简体   繁体   English

Pandas iloc复合切片每第n行

[英]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). 我有一个14行的周期数据帧,即每条记录有14行数据(均值,sdev等),我想为每条记录重复提取第2,第4,第7和第9行(14行) )。 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 我从这里获得了代码的帮助,这对我来说非常有用,但不是多行选择 - Pandas每隔n行

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_ : 你可以使用slicenp.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) . 在此示例中,使用1::14索引数据帧行等效于使用slice(1, df.shape[0], 14)进行索引。

This is fairly generic, you can define any tuple of slice objects and pass to np.r_ . 这是非常通用的,您可以定义任何切片对象元组并传递给np.r_

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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