在pandas中分组后,根据匹配的字符串过滤数据

时间:2017-08-22 10:31:05

标签: python pandas dataframe pandas-groupby

我希望按IdVehicle进行分组,然后只返回其拍卖只有'合并救助'和'合并救助API'值的车辆。拥有这两种类型以外的拍卖的车辆将被过滤掉。我怎样才能做到这一点?

数据:

Id      Vehicle            Auction      Offer
10  1984 Porsche 944     Copart             100
10  1984 Porsche 944     Merged Salvage     200
10  1984 Porsche 944     IAA                140
11  2000 Chevrolet G3500 Copart              80
11  2000 Chevrolet G3500 Merged Salvage     100
11  2000 Chevrolet G3500 Merged Salvage API 120
11  2000 Chevrolet G3500 SVP                130
12  2001 Honda CRV       Merged Salvage      50
12  2001 Honda CRV       Merged Salvage API 100

预期产出:

Id      Vehicle            Auction      Offer
12  2001 Honda CRV       Merged Salvage      50
12  2001 Honda CRV       Merged Salvage API 100

1 个答案:

答案 0 :(得分:1)

我认为df.groupby操作,后跟dfGroupBy.transform应该执行此操作:

In [1097]: status = ['Merged Salvage', 'Merged Salvage API']

In [1098]: df[df.groupby(['Id', 'Vehicle']).Auction\
                 .transform(lambda x: x.isin(status).min())]
Out[1098]: 
   Id         Vehicle             Auction  Offer
7  12  2001 Honda CRV      Merged Salvage     50
8  12  2001 Honda CRV  Merged Salvage API    100

谓词使用pd.Series.isin检查状态。