Search code examples
pythonpandasdata-manipulationfeature-extractionfeature-engineering

Create binary indicator dependent on previous row using Python and Pandas


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!


Solution

  • 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