2017-08-21 167 views
1

这应该是非常容易,但我不能让它工作。python pandas 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 filter one column by more than one value? 
df.loc[df['channel'] == ('sale','fullprice')] 

这是否必须是OR语句?我可以在SQL中使用类似的东西吗?

+3

'df.loc [df ['channel']。isin(['sale','fullprice'])]' – MaxU

+0

非常感谢! – jeangelj

回答

3

存在df.isin(values)测试方法 DataFrame中的每个元素是否包含在值中。 所以,@MaxU在评论中写道,您可以使用

df.loc[df['channel'].isin(['sale','fullprice'])] 

由多个值过滤一列。

+1

''df.loc [df ['channel']。apply(lambda x:x in ['sale','fullprice'])]'也可以。它不如使用'df.isin'简洁,但可以修改,以检查任何折叠条件,这取决于只有一列。 – tipanverella

+1

是的,绝对。 – taras

+0

很棒 - 谢谢你的选择,超好玩! – jeangelj