2017-08-08 78 views
0

的數據是這樣的:如何查找數據框中列的10年平均值?

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是數據幀的名稱。

回答

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 
+0

這10年的平均水平應該是1990年(數據開始,直到2017年後10年)和每年之間(1991年,1992,1993 ..... 2017) –

+0

您的數據始於70年代,而不是80年代。我不明白你在說什麼。按照要求,這確實是平均數十年。 –

+0

1990年1月31日,平均值應爲1980年1月31日 - 1990年1月31日,1990年2月28日的平均值,1980年2月28日平均值爲1980年2月28日等等 –