Python dataframe - 根据列删除连续的行

标签 python pandas dataframe

我需要根据列值删除连续的行。我的数据框如下所示

df = pd.DataFrame({
            "CustID":
                ["c1","c1","c1","c1","c1","c1","c1","c1","c1","c1","c2","c2","c2","c2","c2","c2"],
            "saleValue":
                [10, 12, 13, 6, 4 , 2, 11, 17, 1,5,8,2,16,13,1,4],
             "Status":
                [0, 0, 0, 1, 1 ,1, 0, 0, 1,1,1,1,0,0,1,1]
            
            
    })

dataframe looks like below

  CustID    saleValue   Status
    c1            10    0
    c1            12    0
    c1            13    0
    c1             6    1
    c1             4    1
    c1             2    1
    c1            11    0
    c1            17    0
    c1             1    1
    c1             5    1
    c2             8    1
    c2             2    1
    c2            16    0
    c2            13    0
    c2             1    1
    c2             4    1
    

只有当 Status 为 1 时,我才需要删除每个 CustID 的连续行。你能告诉我最好的方法吗

so the output should look like below.
 

CustID  saleValue   Status
    c1        10          0
    c1        12          0
    c1        13          0
    c1         6          1
    c1        11          0
    c1        17          0
    c1         1          1
    c2         8          1
    c2        16          0
    c2        13          0
    c2         1          1

最佳答案

为整个 DataFrame 创建一个 bool 掩码。

鉴于DataFrame已经按ID分组,找到值为1,上一行也为1,且ID与上一行ID相同的行。这些是要删除的行,因此请保留其余行。

to_drop = (df['Status'].eq(1) & df['Status'].shift().eq(1)  # Consecutive 1s
           & df['CustID'].eq(df['CustID'].shift()))         # Within same ID  

df[~to_drop]

   CustID  saleValue  Status
0      c1         10       0
1      c1         12       0
2      c1         13       0
3      c1          6       1
6      c1         11       0
7      c1         17       0
8      c1          1       1
10     c2          8       1
12     c2         16       0
13     c2         13       0
14     c2          1       1

关于Python dataframe - 根据列删除连续的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64231937/

相关文章:

python - 将计算行添加到多索引的子索引的矢量化方式

python - 从 Pandas DataFrame 中提取样本,保留相同类型的所有值

python - PyCharm 中未显示数据帧头

python-3.x - 值错误 : arrays must all be same length in python using pandas DataFrame

python - 如何使用python将原始数据转换为具有行和列的矩阵?

python - 列表索引超出范围,使用 split()

python - 有效地计算唯一元素的数量 - NumPy/Python

python - 从 python 脚本启动 MySQL

python - 有没有办法从 DataFrame.from_dict 中删除列号和行号?

Python Pandas 子集十六进制字符串,转换为十进制