简体   繁体   English

通过数据框迭代和比较值

[英]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. 我正在尝试查找GVKEY与上一行相同但EXEC_FULLNAME与上一行不同的元素。 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. 我可能会添加一个新的列名FLAG,如果找到它,则该行的FLAG值为1,否则,该FLAG值为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. 您可以使用shift键上下shift数据。 So df.shift will have an NaN in the first row and then otherwise have you data nudged down one row. 因此, df.shift在第一行中将具有NaN ,然后在另一行中将您的数据df.shift

So if your original frame is df : 因此,如果您的原始帧是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 . 将为您提供TrueFalse列。 If you really prefer 1 's and 0 's replace the last line with: 如果您确实更喜欢10则将最后一行替换为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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