lundi 25 mai 2020

NaN in else statement

Please I can't figure out why is function returning me NaN in else statement.

The goal is to get mean of the all goals scored in whole season by team without the last match. If there is only one match in the season, I want to return goals scored in that match.

df.insert(loc = 1, column ="HOME_GOALS_LAST_SEASON", value = 99.9 )

def last_season(team):
        if len(team["HOME_GOALS"] > 1):
            return team["HOME_GOALS"].iloc[:-1].mean()
        else:
            return team["HOME_GOALS"].iloc[0]

df = df.set_index(["HOME", "SEASON"])
df["HOME_GOALS_LAST_SEASON"] = df.groupby(["HOME", "SEASON"]).apply(last_season)
df = df.reset_index()

Aucun commentaire:

Enregistrer un commentaire