Slicing in pandas

https://online.codingblocks.com/app/player/209779/content/201860/8022/lecture

at 8:21 in this video,
movies_titles=movies_titles[[0,1]]

how is this statement working? (on my pc it gives an error-- KeyError: “None of [Int64Index([0, 1], dtype=‘int64’)] are in the [columns]”)

isnt it supposed to be

movie_titles=movie_titles.iloc[:,0:2] ?

hey @devchopra999_11c6416ab7f09bbf ,
are your reading the data correctly , because in the video , there while reading the data they have kept headers = None

means not reading header values , hence in place of them it comes as 0 and 1 , and hence it worked.

what does header=none do? it removes the column heading right? so we can slice a database like an array if it has no headers and otherwise we have to use the iloc function?

there are headers , but instead of names , they are numerical values like 0,1,2,…

yes.

1 Like

This annoying error means that Pandas can not find your column name in your dataframe. Before doing anything with the data frame, use print(df.columns) to see dataframe column exist or not.

print(df.columns)

I was getting a similar kind of error in one of my codes. Turns out, that particular index was missing from my data frame as I had dropped the empty dataframe 2 rows. If this is the case, you can do df.reset_index(inplace=True) and the error should be resolved.