Passing a long list inside the indexing operator might cause readability issues. To help with this, you may save all your column names to a list variable first. The following code achieves the same result as step 1:
>>> cols = ['actor_1_name', 'actor_2_name',
'actor_3_name', 'director_name']
>>> movie_actor_director = movie[cols]
One of the most common exceptions raised when working with pandas is KeyError. This error is mainly due to mistyping of a column or index name. This same error is raised whenever a multiple column selection is attempted without the use of a list:
>>> movie['actor_1_name', 'actor_2_name',
'actor_3_name', 'director_name']
KeyError: ('actor_1_name', 'actor_2_name',
'actor_3_name', 'director_name')
This is a common error to encounter, as it is easy to forget to place the desired columns in a list. You might be wondering what exactly is going on here. The four string names separated by commas are technically a tuple object. Normally, tuples are written with open and closing parentheses, but it isn't necessary:
>>> tuple1 = 1, 2, 3, 'a', 'b'
>>> tuple2 = (1, 2, 3, 'a', 'b')
>>> tuple1 == tuple2
True
Pandas is trying to find a column name exactly equal to the tuple, ('actor_1_name', 'actor_2_name', 'actor_3_name', 'director_name'). It fails and raises a KeyError.