Learner
Learner

Reputation: 672

Check if a value of dataframe doesnt exists in the dictionary using pandas/python

I have this dataframe :

          movieName  Year
            Iron Man  2008
  X-Men Dark Phoenix  2014
      Doctor Strange  2007
          Spider-Man  2014
   x-Men First class  2006
     Captain America  2011

And i have this dictionary

dict = {'Spider-Man':'The amazing Spider-Man', 'X-Men Dark Phoenix': 'X-Men Days of future',
'x-Men First class':'x-Men:The last stand' }

how can i check that the value of dataframe doesnt exists in the dictionary and print the row for exp Iron man and Captain America they dont exist int the dict how can i check this using pandas.

Expected output:

 movieName  Year
    Iron Man  2008
    Captain America  2011

Upvotes: 3

Views: 414

Answers (1)

jezrael
jezrael

Reputation: 862671

First dont use dict for variable, because builten (python code word). You can use Series.isin with keys of dict:

d = {'Spider-Man':'The amazing Spider-Man', 
     'X-Men Dark Phoenix': 'X-Men Days of future',
     'x-Men First class':'x-Men:The last stand' }

df1 = df[~df['movieName'].isin(d.keys())]
print (df1)
         movieName  Year
0         Iron Man  2008
2   Doctor Strange  2007
5  Captain America  2011

Upvotes: 4

Related Questions