找到大于阈值的连续值

  • 本文关键字:连续 阈值 大于 pandas
  • 更新时间 :
  • 英文 :


我有一个熊猫df,如下所示:

YEARMONTH   UNITS_SOLD

2020-01      5
2020-02      10
2020-03      10
2020-04      5
2020-05      5
2020-06      5
2020-07      10
2020-08      10

我正在寻找有2个或更多实例的行,其中出售的NUM_UNITS>=10,并在marker列中将这种情况的第一个实例标记为True。所以我正在努力实现


YEARMONTH   UNITS_SOLD    Marker

2020-01      5           False
2020-02      10          True
2020-03      10          False
2020-04      5           False
2020-05      5           False
2020-06      5           False
2020-07      10          True
2020-08      10          False 
````
Not sure how to proceed here...any inputs will be appreciated.
Thanks!

我们可以执行cumsum创建子组密钥

s = df.UNITS_SOLD.lt(10).cumsum()
out = df[df.UNITS_SOLD.ge(10)].groupby(s)['UNITS_SOLD'].transform('count')
df['Maker'] = df.index.isin(out[out>=2].groupby(s).idxmin())
df
Out[159]: 
YEARMONTH  UNITS_SOLD  Maker
0   2020-01           5  False
1   2020-02          10   True
2   2020-03          10  False
3   2020-04           5  False
4   2020-05           5  False
5   2020-06           5  False
6   2020-07          10   True
7   2020-08          10  False

最新更新