Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

python - Compare upcoming row with previous by index (Pandas)

General idea:

  • iterate over each row in the df (would prefer iterrows method over plain iteration of column)
  • once in the first column (df2[1] == 'Position') is encountered
  • check if the upcoming row == 'Table'
  • if not delete the entire row where the initial 'Position' was found

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] 

question from:https://stackoverflow.com/questions/65924441/compare-upcoming-row-with-previous-by-index-pandas

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...