【问题标题】:i want to drop 2 rows in the dataframe if zero comes in the column如果列中出现零,我想在数据框中删除 2 行
【发布时间】:2019-12-22 02:10:26
【问题描述】:

如果 0 出现在奇数索引上,则使用 pandas 删除前一行和当前行

例子

column1    column2
a              1
b              0
c              2
b              3
e              7
f              0

输出

column1    column2
c              2
b              3

【问题讨论】:

    标签: python-3.x pandas


    【解决方案1】:

    假设您的数据框从 0 开始编入索引

    # Rows with column2 = 0 and on odd index
    idx = df[(df['column2'] == 0) & (df.index % 2 == 1)].index
    
    # The rows above them
    idx = idx.append(idx-1)
    
    # A new dataframe with those rows removed
    result = df.drop(idx)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-04
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 2023-02-17
      相关资源
      最近更新 更多