Pandas-根据条件从DataFrame中选择行

时间:2017-04-26 11:29:34

标签: pandas indexing filtering

df:

category  value   
A         25  
B         10  
A         15  
B         28  
A         18

需要选择满足以下条件的行,
1. category = A和10到20之间的值 2. A以外的类别

1 个答案:

答案 0 :(得分:3)

我认为你需要boolean indexing

df1 = df[(df['category'] == 'A') & (df['value'].between(10,20))]
print (df1)
  category  value
2        A     15
4        A     18

然后:

df2 = df[(df['category'] != 'A') & (df['value'].between(10,20))]
print (df2)
  category  value
1        B     10

或者:

df3 = df[df['category'] != 'A']
print (df3)
  category  value
1        B     10
3        B     28

编辑:加入|的{​​{1}}两个条件,不要忘记在第一个条件下添加or

()