Home > OS >  Create binary indicator dependent on previous row using Python and Pandas
Create binary indicator dependent on previous row using Python and Pandas

Time:04-21

I am coming from the following Excel table:

enter image description here

I want to create a binary indicator indicating cases where the departure airport is not equal the previous arrival airport - basically reconstructing what I did in Excel (Note: "WENN" is equal to "IF" in English). The dataframe is sorted accordingly. What is the best way to do this with python? Is there a way to solve it with pandas?

And lastly, is there a better and more concise technical formulation to ask this question?

Thanks already!

CodePudding user response:

You could use the shift method as:

bin_indicator = df['arr_ap_shed'].shift(1).eq(df['dep_ap_shed'])
bin_indicator[0] = False  # first pos after `shift` is undefined
  • Related