0

I was reading the book - "Python for Data Analysis" and doing code side-by-side on Jupiter notebook. Here is my DataFrame named data :

                 one    three   four
       Ohio       0       2      3
       Colorado   4       6      7
       Utah       8       10    11
       New York   12      14    15

and I used : data.loc[:"one"]. It gives a Keyerror 'one'.
But when I use: data.loc[:"Utah" , "one"]. It doesn't give any error. Can anyone tell me what's going one here.
So the problem is solved.
The error was I was missing a comma(,). I have to do something like this :

data.loc[:,"one"]

but i was doing data.loc[:"one"] which gave error. But we don't have to include "," while we are dealing with more than one column or it will give error( kind of weird syntax ).
eg : do this : data.loc[:"Utah","one"]
not this : data.loc[:,"Utah","one"]

  • 1
    What are you trying to slice? There is no "one" in the index. If you want the columns: `data.loc[:, :"one"]`, or `data["one"]` – mozway May 14 '22 at 11:49
  • 1
    Yeah, that was too early for me to ask the question. The book later guided that for one column I have to use comma(,) after : –  May 14 '22 at 11:51
  • Was your problem solved? – Hamzah May 14 '22 at 13:27
  • 1
    Does this answer your question? [Selection with .loc in python](https://stackoverflow.com/questions/44890713/selection-with-loc-in-python) – Michael Delgado May 14 '22 at 15:05

0 Answers0