如何在数据框中查找列的10年平均值?

时间:2017-08-08 23:35:14

标签: python pandas dataframe

数据如下所示:

Date              Company                     Price EPS   CPI
0 1975-04-30      3M Co                       0     0     53.0
1 1975-04-30      Abbott Laboratories         0     0     53.0
2 1975-04-30      AbbVie Inc                  0     0     53.0
3 1975-04-30      Accenture PLC               0     0     53.0
4 1975-04-30      Activision Blizzard Inc     0     0     53.0

我想计算10年平均收益,并将其除以下一年的价格。如何在Python中做到这一点?我尝试了以下方法: -

CAPE.groupby(['Company','EPS']/10).mean()

但输出格式不正确。 任何帮助将不胜感激

P.S。:CAPE是数据框的名称。

1 个答案:

答案 0 :(得分:0)

我认为这可能会对您有所帮助:

In [79]: df.groupby([(df.Date.dt.year // 10 * 10), 'EPS']).mean()
Out[79]: 
          Price   CPI
Date EPS             
1970 0        0  53.0
1980 0        0  53.0
1990 0        0  53.0
2000 0        0  53.0
2010 0        0  53.0

我对数据进行了一点修改,因此年份的分组会有所不同。

您提供的数据并不足以对其进行测试,因此我制作了一些如下所示的其他数据:

Date,Company,Price,EPS,CPI
1985-01-30,3M Co,0,2,56.0
1986-04-30,3M Co,0,3,93.0
1983-06-30,3M Co,0,4,18.0
1979-04-30,Abbott Laboratories,0,5,52.0
1972-03-30,Abbott Laboratories,0,6,73.0
1971-09-30,Abbott Laboratories,0,7,58.0
1995-04-30,AbbVie Inc,0,8,53.0
2015-04-30,Accenture PLC,0,9,53.0
2005-04-30,Activision Blizzard Inc,0,0,53.0

并执行了以下操作:

In [83]: df=pd.DataFrame.from_csv('t.csv', index_col=None)

In [84]: df.Date = df.Date.apply(lambda x: pd.to_datetime(x))

In [85]: df.groupby([(df.Date.dt.year // 10 * 10), 'EPS']).mean()
Out[85]: 
          Price   CPI
Date EPS             
1970 5        0  52.0
     6        0  73.0
     7        0  58.0
1980 2        0  56.0
     3        0  93.0
     4        0  18.0
1990 8        0  53.0
2000 0        0  53.0
2010 9        0  53.0

好的,假设相同的数据帧,我们可以将索引设置为Date并执行按十年分组的滚动平均值,我相信。这是代码:

In [52]: df = df.set_index('Date')

In [53]: df.groupby(df.index.year // 10 * 10).rolling('3650d').mean()
Out[53]: 
                                 Company  Price  EPS        CPI
Date Date                                                      
1970 1979-04-30      Abbott Laboratories    0.0  5.0  52.000000
     1972-03-30      Abbott Laboratories    0.0  5.5  62.500000
     1971-09-30      Abbott Laboratories    0.0  6.0  61.000000
1980 1985-01-30                    3M Co    0.0  2.0  56.000000
     1986-04-30                    3M Co    0.0  2.5  74.500000
     1983-06-30                    3M Co    0.0  3.0  55.666667
1990 1995-04-30               AbbVie Inc    0.0  8.0  53.000000
2000 2005-04-30  Activision Blizzard Inc    0.0  0.0  53.000000
2010 2015-04-30            Accenture PLC    0.0  9.0  53.000000

十年和公司分组:

In [67]: df.groupby([df.index.year // 10 * 10, df.Company]).rolling('3650d').mean()
    ...: 
Out[67]: 
                                                         Company  Price  EPS  \
Date Company                 Date                                              
1970 Abbott Laboratories     1979-04-30      Abbott Laboratories    0.0  5.0   
                             1972-03-30      Abbott Laboratories    0.0  5.5   
                             1971-09-30      Abbott Laboratories    0.0  6.0   
1980 3M Co                   1985-01-30                    3M Co    0.0  2.0   
                             1986-04-30                    3M Co    0.0  2.5   
                             1983-06-30                    3M Co    0.0  3.0   
1990 AbbVie Inc              1995-04-30               AbbVie Inc    0.0  8.0   
2000 Activision Blizzard Inc 2005-04-30  Activision Blizzard Inc    0.0  0.0   
2010 Accenture PLC           2015-04-30            Accenture PLC    0.0  9.0   

                                               CPI  
Date Company                 Date                   
1970 Abbott Laboratories     1979-04-30  52.000000  
                             1972-03-30  62.500000  
                             1971-09-30  61.000000  
1980 3M Co                   1985-01-30  56.000000  
                             1986-04-30  74.500000  
                             1983-06-30  55.666667  
1990 AbbVie Inc              1995-04-30  53.000000  
2000 Activision Blizzard Inc 2005-04-30  53.000000  
2010 Accenture PLC           2015-04-30  53.000000