使用pandas/python检查字典中是否不存在dataframe的值

p3rjfoxz  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(347)

我有这个数据框:

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

我有这本字典

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' }

我如何检查dataframe的值在字典中不存在,如何打印exp iron man和captain america的行?他们在字典中不存在。我如何使用pandas进行检查。
预期产出:

movieName  Year
    Iron Man  2008
    Captain America  2011
u5rb5r59

u5rb5r591#

首先不要使用 dict 对于变量,因为builten(python代码字)。你可以用 Series.isin 使用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

相关问题