[英]Pandas DataFrame find the max after Groupby two columns and get counts
[英]Pandas groupby two columns and get max value
Pandas 按兩列分組並獲得最大值
我用多索引對數據進行了分組
Model VehicleType VehicleType
100 sedan 278
wagon 109
coupe 2
convertible 1
145 small 19
... ...
zafira sedan 22
small 11
suv 7
convertible 1
coupe 1
我需要使用相應的 model 和 VehicleType 來獲取計數的最大值(右列),如下所示:
Model VehicleType VehicleType
100 sedan 278
145 small 19
... ...
zafira sedan 22
感謝您的解決方案!
使用DataFrameGroupBy.idxmax
按最大值索引索引,然后按 DataFrame.loc 使用DataFrame.loc
:
df = df.loc[df.groupby(level=0)['VehicleType'].idxmax()]
print (df)
VehicleType
Model VehicleType
100 sedan 278
145 small 19
zafira sedan 22
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.