Reputation: 589
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!
Upvotes: 2
Views: 132
Reputation: 13768
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)
Upvotes: 1