Conditionally drop Pandas Dataframe row Conditionally drop Pandas Dataframe row pandas pandas

Conditionally drop Pandas Dataframe row


Use Series.shift twice, and check where num2 equals:

df[df['num2'].shift().ne(df['num2'].shift(-1))]   num1  num20    12    102    13    103    42    114     4    115     5     2


IIUC,

df.loc[df['num2'].diff() != df['num2'].diff(-1)]

Output

   num1  num20    12    102    13    103    42    114     4    115     5     2

if you need all three to match:

df.loc[df['num2'].diff().bfill().rolling(3, center=True).sum().eq(0)]


Just in case you only want to drop rows where the rows just before and just after AND the current row have the same value for the column num2, use :

df[~(df['num2'].eq(df['num2'].shift()) & df['num2'].eq(df['num2'].shift(-1)))]

Here is an example :

   num1  num20    12    101    11    102    13    103     1    264     2     7  # <---- Do you want to drop this value ? if yes, consider Erfan 's solution5     3    26       # if you want to keep it, I proposed another solution b)
import pandas as pddf = pd.DataFrame([    [12, 10],    [11, 10],    [13, 10],    [1, 26],    [2, 7],    [3, 26]], columns=["num1", "num2"])a = df[df['num2'].shift().ne(df['num2'].shift(-1))] # Erfan 's solutionb = df[~(df['num2'].eq(df['num2'].shift()) & df['num2'].eq(df['num2'].shift(-1)))]print(a)print(b)

Output :

# a   num1  num20    12    102    13    103     1    264     2     75     3    26# b   num1  num20    12    102    13    103     1    265     3    26