我有以下数据帧:
2012 2013 2014 2015 2016 2017 2018 Kategorie
0 5.31 5.27 5.61 4.34 4.54 5.02 7.07 Gewinn pro Aktie in EUR
1 13.39 14.70 12.45 16.29 15.67 14.17 10.08 KGV
2 -21.21 -0.75 6.45 -22.63 -7.75 9.76 47.52 Gewinnwachstum
3 -17.78 2.27 -0.55 3.39 1.48 0.34 NaN PEG
现在,我只选择KGV
行:
df[df["Kategorie"] == "KGV"]
输出:
2012 2013 2014 2015 2016 2017 2018 Kategorie
1 13.39 14.7 12.45 16.29 15.67 14.17 10.08 KGV
如何计算过去五年的mean()
(本例中为2016,15,14,13,12)?
我试过了
df[df["Kategorie"] == "KGV"]["2016":"2012"].mean()
但这带来了TypeError
。为什么我不能在这里切片?你知道吗
loc
支持这种类型的切片(从左到右):请注意,这并不一定意味着2012年、2013年、2014年、2015年和2016年。这些是字符串,因此它表示
df['2012']
和df['2016']
之间的所有列。中间可能有一个名为foo
的列,它将被选中。你知道吗不知道为什么过去五年是2012-2016年(它们似乎是第一个五年)。尽管如此,要找到2012-2016年
'KGV'
的平均值,您可以使用我用了
filter
和iloc
相关问题 更多 >
编程相关推荐