[英]How do I use Pandas to get the sum of 2 columns based on the value of another column in a dataframe
[英]How to sum two dataframe columns based on another column value
數據框負責人我被困在使用下面鏈接的英國超級聯賽數據集的 pandas 的個人數據分析項目上。 HY 列代表主場黃牌,AY 代表客場黃牌。
如何找到整個賽季收到最多黃/紅牌的球隊? 我已經嘗試過 groupby HomeTeam/AwayTeam 列,但結果並不完全應該是這樣。
編輯:我已經嘗試過了,但它非常冗長: df.groupby("HomeTeam")['HY'].sum().sort_values(ascending =False) + df.groupby("AwayTeam")['HY'] .sum().sort_values(升序=假)
https://www.kaggle.com/datasets/irkaal/english-premier-league-results
df['cards'] = df['AY']+df['HY']
u = df.groupby('teamname')['cards'].max()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.