简体   繁体   中英

Iterate and compare values through a dataframe

I have a dataframe which looks like :

coperal      EXEC_FULLNAME    GVKEY   YEAR                               
5623         David P. Storch   1004   1992
5623         David P. Storch   1004   1993
5623         David P. Storch   1004   1994
5623         David P. Storch   1004   1995
5623         David P. Storch   1004   1996
5623         David P. Storch   1004   1997
5623         David P. Storch   1004   1998
5623         David P. Storch   1004   1999
5623         David P. Storch   1004   2000
5623         David P. Storch   1004   2001

I am trying to find elements that the GVKEY is the same as the previous row but the EXEC_FULLNAME is different from the previous row. I might add a new column name FLAG, if I found it, then the FLAG value of that row is 1, if not then the FLAG value is 0.

Could anyone so kind to help me with it?

Thanks a lot!

You can use shift to nudge your data up or down a row. So df.shift will have an NaN in the first row and then otherwise have you data nudged down one row.

So if your original frame is df :

first_condition = df['GVKEY'] == df['GVKEY'].shift()
second_condition = df['EXEC_FULLNAME'] !=  df['EXEC_FULLNAME'].shift()
df['FLAG'] = first_condition & second_condition

will get you a column of True and False . If you really prefer 1 's and 0 's replace the last line with:

df['FLAG'] = np.where(first_condition & second_condition, 1, 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM