Ecko
Ecko

Reputation: 115

Compare upcoming row with previous by index (Pandas)

General idea:

Dataframe:

      1           2
0   Position    random1
1   12345       random2
2   12345       random3
3   Position    random4
4   Table       random5
5   12345       random6
6   12345       random7

Desired result:

     1            2
0   12345       random2
1   12345       random3
2   Position    random4
3   Table       random5
4   12345       random6
5   12345       random7

Pseudo/code:

import pandas as pd

info = {1: ['Position','12345','12345','Position', 'Table', '12345','12345'],
        2: ['random1','random2','random3','random4','random5','random6','random7']
        }

df2 = pd.DataFrame(info, columns = [1,2])

for indx,row in df.iterrows():
        if df.loc[indx,(df[1] == 'Position') & df.loc[indx+1,(df[1] != 'Table')]:
                  del df.loc[indx, 1] 

Upvotes: 0

Views: 34

Answers (2)

Ecko
Ecko

Reputation: 115

Just to add upon the given question with a full answer:

  • where the strings are targeted (thanks to @Quang Hoang)
  • and deleting it straight away

code:

df.drop(df.loc[(df[1] == 'Position') & (df[1].shift(-1) != 'Table')].index, inplace=True)

Upvotes: 0

Quang Hoang
Quang Hoang

Reputation: 150735

Do not iterate when possible:

mask = ~(df[1].eq('Position') & df[1].shift(-1).ne('Table'))
df[mask]

Output:

          1        2
1     12345  random2
2     12345  random3
3  Position  random4
4     Table  random5
5     12345  random6
6     12345  random7

Upvotes: 1

Related Questions