Search code examples
pythonstringcountlag

Filling Column based on lag column value - python


I have a data table:

Team vpos hpos
home
away
away
away
home
home
away

I would like to add a number the number 1 in vpos or hpos where the team switches to signify a change in position. I figured I would use some type of lag but I do not know how to do that in python. I would like the final table to look like this:

Team vpos hpos
home 1
away 1
away
away
home 1
home
away 1

Any direction would help. Thank you!


Solution

  • If your data table is a Pandas dataframe df then you could try:

    m = df["Team"].shift() != df["Team"]
    df.loc[m & df["Team"].eq("away"), "vpos"] = 1
    df.loc[m & df["Team"].eq("home"), "hpos"] = 1
    

    Result for df

       Team
    0  home
    1  away
    2  away
    3  away
    4  home
    5  home
    6  away
    

    is

       Team  vpos  hpos
    0  home   NaN   1.0
    1  away   1.0   NaN
    2  away   NaN   NaN
    3  away   NaN   NaN
    4  home   NaN   1.0
    5  home   NaN   NaN
    6  away   1.0   NaN