1

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!

Paul1911
  • 163
  • 10
  • 1
    You may have a look at the [shift](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.shift.html) method. If you shift your `arr_ap_shed` one pos further, then you can check for equality on the index basis against `dep_ap_shed`. This is a fairly simple example, but even so, please always add your mock data to the question. – deponovo Apr 20 '22 at 15:49

1 Answers1

1

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
deponovo
  • 1,114
  • 7
  • 23