如何像在SQL中一样使用'in'和'not in'过滤Pandas数据帧
我怎样才能达到SQL IN和的等效NOT IN? 我有一个包含所需值的列表。这是场景: df = pd.DataFrame({'countries':['US','UK','Germany','China']}) countries = ['UK','China'] # pseudo-code: df[df['countries'] not in countries] 我目前的做法如下: df = pd.DataFrame({'countries':['US','UK','Germany','China']}) countries = pd.DataFrame({'countries':['UK','China'], 'matched':True}) # IN df.merge(countries,how='inner',on='countries') # NOT IN not_in = df.merge(countries,how='left',on='countries') not_in = not_in[pd.isnull(not_in['matched'])] 但这似乎是一个可怕的冲突。有人可以改进吗?