0

I have an issue with using the 'loc' function in Python. I would like to extract a specific row from my dataframe 'df', which contains movie titles along with their respective information. The problem is that when I use 'df.loc['movie title']', I receive the following error as if it doesn't exist in the DataFrame, even though it does. Let me show you an example:

df.set_index('title')

And here i receive the correct output

enter image description here

as you can see Toy Story in in DataFrame df but when i try to print all his rae with loc i receive this error: enter image description here

I use pd.loc['Toy Story'] but python say that in my df there isn't any 'Toy story' label Can someone give me an explanation of this?

  • did you save the dataframe after setting the index? if yes, can print `df.index` and show us the output – Jeril Aug 02 '23 at 08:44
  • I don't think that i "save" the dataframe, i just set a new index in order to have the title colum as new index and then print it. I try to print df.index and the answer is : RangeIndex(start=0, stop=44691, step=1). – Alessandro Robuschi Aug 02 '23 at 08:54
  • posted the answer, please check – Jeril Aug 02 '23 at 08:57
  • `df.loc('Toy Story')` means that you want to get a `loc` accessor for the 'Toy Story's **axis**, use `df.loc['Toy Story']` (square brackets). – mozway Aug 02 '23 at 09:09

1 Answers1

0

You need to save the changes to the dataframe. Can you try the following:

df = df.set_index('title')
Jeril
  • 7,858
  • 3
  • 52
  • 69