Pandas iloc复合切片每第n行

BAC*_*C83 3 python indexing slice dataframe pandas

我有一个14行的周期数据帧,即每条记录有14行数据(均值,sdev等),我想为每条记录重复提取第2,第4,第7和第9行(14行) ).我的代码是:

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

这不起作用

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

我从这里获得了代码的帮助,这对我来说非常有用,但不是多行选择 - Pandas每隔n行

我可以提取几个不同的切片并组合,但感觉可能有更优雅的解决方案.

有任何想法吗?

WeN*_*Ben 6

使用:

df[np.isin(np.arange(len(df))%14,np.array([1,3,6,8]))]
Run Code Online (Sandbox Code Playgroud)