warning, KeyError on .loc

Question:

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?

Asked By: Sepid

||

Answers:

.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

Answered By: Pranav Hosangadi
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.