数据框列的平均值 [英] Average of Dataframe columns
问题描述
我想获取多年来每个国家的平均GDP,列2006、2007 ... 2015包含GDP数据...我的代码返回一个错误,该错误意味着mean(axis = 1)至少需要1个变量,并且已经为其分配了1 ...这很奇怪..我也发现我们使用均值而不是avg很奇怪,但是却找不到groupby的avg函数
I want to get the average GDP of each country across the years, the columns 2006, 2007...2015 contain the GDP numbers... My code returns an error that mean(axis=1) needs at least 1 variable, and 1 has been assign to it... which is weird..I also find it weird that we are using mean instead of avg, but coulnd't find an avg function for groupby
这是我的代码
Top15 = ANSWER
Top15 = Top15[['Country', '2006', '2007', '2008', '2009', '2010',
'2011', '2012', '2013', '2014', '2015']]
return Top15.groupby('Country').agg({"avg": np.mean(axis=1)})
推荐答案
GroupBy
在这里不是必需的,因为您正在执行计算而不是聚合.您可以只使用 pd.DataFrame.mean
.这是一个最小的示例:
GroupBy
is not necessary here as you are performing a calculation rather than an aggregation. You can just use pd.DataFrame.mean
. Here's a minimal example:
df = pd.DataFrame({'Country': ['UK', 'US'],
'2006': [1, 2],
'2007': [3, 4],
'2008': [5, 6]})
df['mean'] = df[['2006', '2007', '2008']].mean(1)
print(df)
2006 2007 2008 Country mean
0 1 3 5 UK 3.0
1 2 4 6 US 4.0
这篇关于数据框列的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!