我的数据框有3列:年。主要原因,死亡。我想按主要原因查找每年的死亡总数。我做了以下事情:
totalDeaths_Cause = df.groupby(["Year", "Leading Cause"])["Deaths"].sum()
结果是:
The total number of deaths for :
Year Leading Cause
2009 Hypertension 26
2010 All Other Causes 2140
2011 Cerebrovascular Disease 281
Immunodeficiency 70
Parkinson Disease 180
2012 Cerebrovascular Disease 102
Disease1 183
Diseases of Heart 76
2013 Cerebrovascular Disease 386
Parkinson Disease 372
Self-Harm 17
Name: Deaths, dtype: int64
现在,我想每年获取最大的2个值(用于死亡),以及导致以下原因的主要原因:
The total number of deaths for :
Year Leading Cause
2009 Hypertension 26
2010 All Other Causes 2140
2011 Cerebrovascular Disease 281
Parkinson Disease 180
2012 Disease1 183
Cerebrovasular disease 102
2013 Cerebrovascular Disease 386
Parkinson Disease 372
在此先感谢您的帮助!
答案 0 :(得分:0)
让我们这样做
df=df.sort_values().groupby(level=0).tail(1)