jea*_*elj 15 python filter loc pandas
这应该是非常容易的,但我不能让它工作.
我想在两个值上过滤我的数据集.
#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')]
Run Code Online (Sandbox Code Playgroud)
这必须是一个OR声明吗?我可以在SQL中使用吗?
tar*_*ras 31
有一种df.isin(values)方法可以测试是否DataFrame包含其中的每个元素values.所以,正如@MaxU在评论中所写,你可以使用
df.loc[df['channel'].isin(['sale','fullprice'])]
Run Code Online (Sandbox Code Playgroud)
按多个值过滤一列.