茅侃侃
我相信需要rename_axis使用add和DataFrame使用相同名称的 MultiIndex reset_index:leaguesFinal = pd.DataFrame({ 'Home Team': ['b','a','a','c','b','a'], 'Away Team': ['a','b','c','a','a','b'], 'season': ['2010/2011'] * 3 + ['2012/2013'] * 3, 'home_team_goal': [1,2,3,4,3,2], 'away_team_goal': [4,6,7,8,2,1]})print (leaguesFinal) Home Team Away Team season home_team_goal away_team_goal0 b a 2010/2011 1 41 a b 2010/2011 2 62 a c 2010/2011 3 73 c a 2012/2013 4 84 b a 2012/2013 3 25 a b 2012/2013 2 1home_goals = leaguesFinal.groupby(['Home Team', 'season'])['home_team_goal'].sum()away_goals = leaguesFinal.groupby(['Away Team', 'season'])['away_team_goal'].sum()print (home_goals)Home Team season a 2010/2011 5 2012/2013 2b 2010/2011 1 2012/2013 3c 2012/2013 4Name: home_team_goal, dtype: int64print (away_goals)Away Team season a 2010/2011 4 2012/2013 10b 2010/2011 6 2012/2013 1c 2010/2011 7Name: away_team_goal, dtype: int64a = home_goals.rename_axis(['Team','season'])b = away_goals.rename_axis(['Team','season'])df = (a.add(b, fill_value=0)).reset_index(name='both')print (df) Team season both0 a 2010/2011 9.01 a 2012/2013 12.02 b 2010/2011 7.03 b 2012/2013 4.04 c 2010/2011 7.05 c 2012/2013 4.0