Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

warning, KeyError on .loc

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?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

.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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading