我有一个类似下面的DF:
>>> df
order_received
0 1
1 1
2 0
3 0
4 1
5 0
6 0
我想把这个分成(1), (1), (0, 0, 1), (0, 0)
,即每当出现1
时就分割系列。我该怎么做?
cumsum
对列进行反向排序,然后使用groupby
+list
。df.groupby(df.order_received.iloc[::-1].eq(1).cumsum())['order_received'].apply(list).iloc[::-1]
Out[419]:
order_received
3 [1]
2 [1]
1 [0, 0, 1]
0 [0, 0]
Name: order_received, dtype: object
第二种方法是使用shift
加上cumsum
。
df.groupby(df.order_received.shift().fillna(0).cumsum())['order_received'].apply(list)
Out[432]:
order_received
0.0 [1]
1.0 [1]
2.0 [0, 0, 1]
3.0 [0, 0]
Name: order_received, dtype: object
.eq()
:) - An SO User