Skip to main content
2 of 3
added 31 characters in body
Chuck
  • 3.9k
  • 7
  • 45
  • 82

There is a simple and more efficient (2.5* quicker than current answer) way to achieve this. For your dataframe df and dictionary of watchlists watchlist, you can use df.loc with multiple conditions.

First, create placeholder column:

df['Watched'] = 'No' Episode Number Rating Series Watched 0 4 Days Out 2.90 9.1 Breaking Bad (2008) No 1 Buyout 5.60 9.0 Breaking Bad (2008) No 2 Pilot 1.10 9.0 Breaking Bad (2008) No 3 Dog Fight 1.12 9.0 Suits (2011) No 4 We're Done 4.70 9.0 Suits (2011) No 5 Privilege 5.60 8.9 Suits (2011) No 6 Pilot 1.10 8.9 Suits (2011) No 

Then iterate over watchlist:

for i, v in watchlist.iteritems(): df.loc[(df['Number'].isin(v)) & (df['Series'] == i), 'Watched'] = 'yes' 

This gives df:

 Episode Number Rating Series Watched 0 4 Days Out 2.90 9.1 Breaking Bad (2008) yes 1 Buyout 5.60 9.0 Breaking Bad (2008) No 2 Pilot 1.10 9.0 Breaking Bad (2008) yes 3 Dog Fight 1.12 9.0 Suits (2011) No 4 We're Done 4.70 9.0 Suits (2011) yes 5 Privilege 5.60 8.9 Suits (2011) yes 6 Pilot 1.10 8.9 Suits (2011) No 

No need for extra columns / concatenation or dropping columns.

Total time this answer = 0.00800013542175 s Total time accepted answer = 2.624944121596675 s 
Chuck
  • 3.9k
  • 7
  • 45
  • 82