![](/img/trans.png)
[英]pandas how to sort groupby by group sizes while aggregating on another column
[英]pandas: how to skip a row while aggregating a column?
我有以下DataFrame:
actor Daily Total actor1 actor2
Day
2019-01-01 25 10 15
2019-01-02 30 15 15
Total 55 25 30
我想按以下方式計算每列的平均值:
actor Daily Total actor1 actor2
Day
2019-01-01 25 10 15
2019-01-02 30 15 15
Total 55 25 30
Avg 27.5 12.5 15.0
但是,如果我使用df.loc['Avg'] = df.mean()
,它也會考慮“總計”行。 在計算平均值時如何跳過“總計”行?
用drop
刪除行:
df.loc['Avg'] = df.drop('Total').mean()
或選擇所有沒有倒數的行:
df.loc['Avg'] = df.iloc[:-1].mean()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.