Python 对多个值使用逻辑运算符

Python 对多个值使用逻辑运算符,python,dataframe,logical-operators,Python,Dataframe,Logical Operators,我想使用逻辑运算符提取多个值,但到目前为止,我还没有找到一种简单的方法。我尝试传递希望作为列表排除的值,但它引发了一个错误。因此,这就是我排除这些值的方法: droppedd = dropped[dropped['date'] == '2021-04-04'] droppedd = droppedd[droppedd['location'] != 'World'] droppedd = droppedd[droppedd['location'] != 'Europe'] droppedd = d

我想使用逻辑运算符提取多个值,但到目前为止,我还没有找到一种简单的方法。我尝试传递希望作为列表排除的值,但它引发了一个错误。因此,这就是我排除这些值的方法:

droppedd = dropped[dropped['date'] == '2021-04-04']
droppedd = droppedd[droppedd['location'] != 'World']
droppedd = droppedd[droppedd['location'] != 'Europe']
droppedd = droppedd[droppedd['location'] != 'North America']
droppedd = droppedd[droppedd['location'] != 'European Union']
droppedd = droppedd[droppedd['location'] != 'South America']
droppedd = droppedd[droppedd['location'] != 'Asia']
Droppedd是输出变量的名称。是否有更简单的方法排除这些值

droppedd = dropped[dropped['date'] == '2021-04-04']
droppedd = droppedd[droppedd['location'] not in ['World', 'Europe', 'North America' , 'European Union', 'South America', 'Asia' ]]

同样,DROPED是用一个d写的。

请下次提供示例数据帧

import pandas as pd

df1 = pd.DataFrame({'date': ['2021-04-04', '2021-04-05', '2021-04-04', '2020-04-04', '2021-05-04', '2021-04-12', '2021-04-12', '2021-04-04'],
                        'location': ['World', 'Europe', 'North America', 'European Union', 'South America', 'Asia', 'Africa', 'Oceania'],
                        'x': [1, 2, 3, 4, 5, 6, 7, 8],
                        'y': [1, 2, 3, 4, 5, 6, 7, 8],
                        'z': [1, 2, 3, 4, 5, 6, 7, 8]})


l_keep1= ['2021-04-04']
l_drop2 = ['World', 'Europe', 'North America', 'European Union','South America', 'Asia']

df2 = df1.loc[df1['date'].isin(l_keep1)]
df3 = df2.loc[~df2['location'].isin(l_drop2)]

我知道是的,但我用drop表示另一个变量。这就是我在这里用这种方式的原因。很酷的动机,还是很糟糕的做法。然而,这是否回答了你的问题?