可以使用Python中的pandas和numpy库来解决这个问题。下面是相关的代码示例:
import pandas as pd
import numpy as np
# 创建一个包含品牌、年份和毛利的数据集
df = pd.DataFrame({'Brand': ['A', 'B', 'C', 'A', 'B', 'C'],
'Year': [2018, 2018, 2018, 2019, 2019, 2019],
'Gross Profit': [100, 200, 150, 120, 180, 160]})
# 按品牌和年份对毛利求和
profit_by_brand_year = df.groupby(['Brand', 'Year']).agg({'Gross Profit': np.sum})
# 找到每年度最赚钱的品牌
most_profitable = profit_by_brand_year.groupby(['Year']).idxmax()
# 打印每年度最赚钱的品牌和对应的毛利值
for year, (brand, profit) in most_profitable.iterrows():
print("Year {}: Most profitable brand is {}, with a gross profit of ${}.".format(year, brand[0], profit[0]))
执行以上代码,将会输出每年度最赚钱的品牌和对应的毛利值,例如:
Year 2018: Most profitable brand is B, with a gross profit of $200.
Year 2019: Most profitable brand is B, with a gross profit of $180.