如何根据价值划分熊猫系列?

时间:2019-05-10 00:39:40

标签: python pandas

我有一个看起来像下面的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时都将系列拆分。我该怎么办?

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