我希望按Id
和Vehicle
进行分组,然后只返回其拍卖只有'合并救助'和'合并救助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
答案 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
检查状态。