根据下一行中的值删除行| Pandas



我需要编写一个代码,根据它下面的行中的值删除行。

目前,我使用以下代码,对于大型数据集(数万行),它非常慢,因为它遍历所有行:

def f(df):
current_row = 0
max_row = len(df.index) - 1
row_indexes_to_be_dropped = []
for index, row in df.iterrows():
if current_row == max_row:  # If the current row is the last, check cond1 and cond2 on current row and decide if it will be dropped
if (row['cond1'] == 'False') or (row['cond2'] == 'False'):
row_indexes_to_be_dropped.append(index)
elif df.iloc[current_row + 1]['cond2'] == 'False':  # If the current row is not the last, check cond2 on next row and decide if the current row will be dropped
row_indexes_to_be_dropped.append(index)
current_row += 1
df = df.drop(row_indexes_to_be_dropped)
return df
df_grouped = df_more_records.groupby('ID').apply(f)

是否有一种方法可以用整个向量的函数来写这个?

谢谢。

尽量不要使用循环。您没有提供一个完整的工作示例,但我将这样做:

mask = (df['cond1'] == 'False') or (df['cond2'] == 'False')
df.drop(df.index[mask.shift(1, fill_value=False).values.flatten()])
  1. 使用itertuples代替iterrows,这会加快书写速度

    for row in df.itertuples():Value = row.column_name

  2. 你可以使用过滤器代替行字符

    df_filter = df。Loc [df['column_name'] = condition]

  • 没有样本数据,所以生成简单的数据来演示
  • 使用shift(-1)测试下一行
  • drop()中符合条件的行
import numpy as np
import pandas as pd
df = pd.DataFrame({"value":np.random.randint(1,3,10)})
# drop rows where value in next row is 1
df.drop(df.loc[df["value"].shift(-1).eq(1)].index)

最新更新