Home > database >  Filling Column based on lag column value - python
Filling Column based on lag column value - python

Time:12-15

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!

CodePudding user response:

I'm assuming you use Pandas dataframe:

tmp = (df["Team"] != df["Team"].shift()).cumsum()

df["vpos"] = df.groupby(tmp)["Team"].transform(
    lambda x: [1]   [np.nan] * (len(x) - 1) if x.iat[0] != "home" else np.nan
)

df["hpos"] = df.groupby(tmp)["Team"].transform(
    lambda x: [1]   [np.nan] * (len(x) - 1) if x.iat[0] == "home" else np.nan
)

df = df.fillna("")

print(df)

Prints:

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

CodePudding user response:

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
  • Related