我想收集满足条件的细分。我有下面的解决方案。可以简化吗?
import numpy as np, pandas as pd
# test data:
ser= pd.Series([1,7,8,3,4,5,6,23,45,2,3,5,8,33])
crit= ser.le(10) # it could be anything
s1= pd.Series(np.where(crit, np.nan, np.arange(len(ser))))
if np.isnan(s1.iloc[0]):
s1.iloc[0]=0
s1=s1.ffill()
s1[~crit]= np.nan
print("---\n")
for k,v in ser.groupby(s1):
if not np.isnan(k):
print(f"segment:\n{v}\n")
segment:
0 1
1 7
2 8
3 3
4 4
5 5
6 6
dtype: int64
segment:
9 2
10 3
11 5
12 8
dtype: int64
编辑:我需要上面显示的索引。