Country Region Hemisphere Crop Years \
18 United States Of America Temperate Northern hemisphere Maize 2011
22 United States Of America Temperate Northern hemisphere Maize 2015
7 Argentina Temperate Southern hemisphere Soybean 2010
2 Argentina Temperate Southern hemisphere Soybean 2002
5 Argentina Temperate Southern hemisphere Maize 2010
11 United States Of America Temperate Northern hemisphere Soybean 2010
Actual Predicted error \
18 8.856633 8.709798 1.657906
22 10.277621 10.646027 3.584548
7 2.903666 2.528243 12.929296
2 2.604214 2.308457 11.356881
5 7.787116 6.045908 22.360111
11 2.898108 2.833479 2.230042
f-score
18 0.574185
22 0.711111
7 0.189560
2 0.269231
5 0.709091
11 0.627960
我希望按作物汇总上面的示例数据框,同时按年度平均值。我这样做:
df[['Actual', 'Predicted']].groupby(df[['Crop', 'Years']]).mean()
但是,我收到此错误: *** ValueError:Grouper for''不是一维的
如何解决这个问题?
答案 0 :(得分:2)
df[['Actual', 'Predicted']]
只有两列(Actual
和Predicted
),因此您无法按Crop
和Years
对该数据框进行分组。
我认为您首先需要按裁剪和年份进行分组,然后选择要计算平均值的列:
df.groupby(['Crop', 'Years'])[['Actual', 'Predicted']].mean()