九州编程
这是另一个更符合您预期输出的解决方案。df = pd.DataFrame({'Year': [1978,1990,1990,1990,1998,1998,1998,1998,1998], 'Month': [1,1,2,2,1,1,2,3,1], 'Region': ['South','North','South','Mid West','South','North','South','South','Mid West'], 'Value' : [1,22,33,12,1,12,2,4,2]})#DataFrame Result Year Month Region Value0 1978 1 South 11 1990 1 North 222 1990 2 South 333 1990 2 Mid West 124 1998 1 South 15 1998 1 North 126 1998 2 South 27 1998 3 South 48 1998 1 Mid West 2要运行的代码:df1 = df.groupby(['Month','Region']).sum()df1 = df1.drop('Year',axis=1)df1 = df1.sort_values(['Month','Region'])#Final ResultMonth Region Value1 Mid West 21 North 341 South 22 Mid West 122 South 353 South 4