Home > Net >  warning, KeyError on .loc
warning, KeyError on .loc

Time:01-05

I used the .loc in this code and it works fine win_data = df.loc[df['HOME_TEAM_WINS'] == 1] but when I use it win_data['SMA5'] = win_data.loc['PTS_home'].rolling(5).mean() I get KeyError! if I don't put the .loc it works fine but I get the warning! Where did I go wrong?

CodePudding user response:

.loc wants the first index to be the row index. When you do win_data["PTS_home"], you get the "PTS_home" column of the dataframe. The equivalent .loc syntax would be win_data.loc[:, "PTS_home"] (i.e. all rows, and "PTS_home" column)

df.loc[df['HOME_TEAM_WINS'] == 1] works because you're using. a different kind of indexing, viz. boolean indexing to extract only those rows for which the "HOME_TEAM_WINS" column is 1

  • Related