r/PythonLearning • • Sep 13 '25

Key error in pandas

Post image

What is the exact problem with the given line of code throwing key error

0 Upvotes

5 comments sorted by

2

u/PolyPorcupine Sep 13 '25

You have a bracket problem, when you call data by loc , call data.loc[[indexX],[indexY1,indexY2]] enclose everything in brackets and close the X index bract.

1

u/carticka_1 Sep 13 '25

It didn't work, also in the book by Wes McKinney same syntax is given that I have used.

1

u/PureWasian Sep 13 '25

The provided syntax worked for me. Can you add the following to check for whitespace issues?

1

u/PureWasian Sep 13 '25 edited Sep 13 '25

I'm only able to reproduce your exact issue and stack trace by introducing a typo on 'Alpha' or 'Juli' so my guess is you are either instantiate it with some hidden whitespace before the column name, or (less likely) you are using some inconsistent ascii characters for 'l' or 'i' in one place or the other that are very similar but not exactly a match.

2

u/carticka_1 Sep 15 '25

Gladly I found the cause for error, I just re- run the above cell that consisted the Dataframe and it finally the cell with print statement and it worked.