如何在Python中过滤同一行代码中两个不同列(系列)的值?

如何在Python中过滤同一行代码中两个不同列(系列)的值?,python,pandas,series,Python,Pandas,Series,我是Python新手,我的Jupyter笔记本上运行着以下代码: import pandas as pd import matplotlib.pyplot as plt import seaborn as sb %matplotlib inline fields = ['Leisure', 'Spa', 'Market', 'Meal Plan Code'] myfile = pd.read_csv('extras/ExtrasSpending.csv', sep=',', encoding

我是Python新手,我的Jupyter笔记本上运行着以下代码:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sb
%matplotlib inline

fields = ['Leisure', 'Spa', 'Market', 'Meal Plan Code']
myfile = pd.read_csv('extras/ExtrasSpending.csv', sep=',', encoding =  'latin-1', skipinitialspace = True, usecols = fields)
下面是运行myfile.head的输出

我的下一行代码如下所示:

mySelectedData = myfile.loc[myfile['Market'].isin(['United Kingdom','Germany'])]
当我运行mySelectedData.head来检查结果时,这一切正常。现在,我需要在数据的另一列上添加另一个过滤器;我只想将“膳食计划代码”过滤为“AI”

我尝试了这一点,但它不起作用,尽管Python不会向我抛出错误消息:

mySelectedData = myfile.loc[myfile['Market'].isin(['United Kingdom','Germany'])],myfile.loc[myfile['Meal Plan Code'].isin(['AI'])]
如何构造“mySelectedData”对象,使其同时考虑这两个条件?

这应该可以:

mySelectedData = myfile.loc[(myfile['Market'].isin(['United Kingdom','Germany']))&(myfile['Meal Plan Code'].isin(['AI']))]

伟大的谢谢,是的!
mySelectedData = myfile.loc[(myfile['Market'].isin(['United Kingdom','Germany']))&(myfile['Meal Plan Code'].isin(['AI']))]