我有一个看起来像下面的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
时都将系列拆分。我该怎么办?
答案 0 :(得分:5)
您可以按相反的顺序使用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