我有一个熊猫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