基于OR AND的熊猫过滤

时间:2019-07-29 20:35:53

标签: pandas

我正在尝试像这样过滤熊猫df中的行:

df1= df0[(df0.col1=='a' ) | (df0.col2=='b' & df0.col3=='c')]

我相信我使用了适当的括号,但是我得到了:

cannot compare a dtyped [object] array with a scalar of type [bool]

基本上,如果OR(b&C)为true,则是我想要的条件

1 个答案:

答案 0 :(得分:1)

Boolean indexing

  

另一种常见的操作是使用布尔向量来过滤   数据。操作员是:为或,&为与,和〜为非。这些   必须使用括号分组,因为默认情况下,Python将   计算df.A> 2和df.B <3的表达式作为df.A>(2&   df.B)<3,而所需的评估顺序为(df.A> 2)&(df.B <   3)。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Google.Cloud.Firestore;

namespace FirestoreAPI.Repository.Restaurants
{
    [FirestoreData]
    public class Restaurant
    {   
        [FirestoreProperty]
        public decimal avgRating { get; set; }
        [FirestoreProperty]
        public string category { get; set; }
        [FirestoreProperty]
        public string city { get; set; }
        [FirestoreProperty]
        public string name { get; set; }
        [FirestoreProperty]
        public int numRatings { get; set; }
        [FirestoreProperty]
        public int price { get; set; }
    }
}