簡體   English   中英

如何對 pandas 中一列的一個值進行分組?

[英]How to groupby one value of a column in pandas?

我有兩列看起來像這樣:

Vehicle Brand      Reason of repair

Alfa Romeo         Brakes not working
Honda              Annual Service
BMW                Other
Alfa Romeo         Electrical issues
Honda              Paint
Alfa Romeo         Annual service
Alfa Romeo         Annual service

我只想按Alfa Romeo分組並計算Reasons of repair

由於您只關心一個品牌,只需使用loc過濾它並獲取value_counts()

df.loc[df['Vehicle Brand'] == 'Alfa Romeo', 'Reason of repair'].value_counts()

# Annual service        2
# Brakes not working    1
# Electrical issues     1
# Name: Reason of repair, dtype: int64

如果你真的想要groupby ,獲取所有品牌的groupby.value_counts()和 select Alfa Romeo

df.groupby('Vehicle Brand')['Reason of repair'].value_counts().loc['Alfa Romeo']

# Reason of repair
# Annual service        2
# Brakes not working    1
# Electrical issues     1
# Name: Reason of repair, dtype: int64

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM