python-值列表的筛选器
这应该非常容易,但我无法让它工作 我想根据两个或多个值筛选数据集python-值列表的筛选器,python,pandas,filter,loc,Python,Pandas,Filter,Loc,这应该非常容易,但我无法让它工作 我想根据两个或多个值筛选数据集 #this works, when I filter for one value df.loc[df['channel'] == 'sale'] #if I have to filter, two separate columns, I can do this df.loc[(df['channel'] == 'sale')&(df['type']=='A')] #but what if I want to fil
#this works, when I filter for one value
df.loc[df['channel'] == 'sale']
#if I have to filter, two separate columns, I can do this
df.loc[(df['channel'] == 'sale')&(df['type']=='A')]
#but what if I want to filter one column by more than one value?
df.loc[df['channel'] == ('sale','fullprice')]
这是否必须是一个或声明?我可以在SQL中使用?有一种方法可以测试
数据框中的每个元素是否包含在值中。
因此,@MaxU在评论中写道,您可以使用
df.loc[df['channel'].isin(['sale','fullprice'])]
按多个值筛选一列。df.loc[df['channel'].isin(['sale','fullprice'])]
df.loc[df['channel'].apply(λx:x在['sale','fullprice'])
也可以使用。它不像使用df.isin
那样简洁,但是可以根据一列进行修改以检查任何复杂的条件。是的,绝对可以。