如何使用pandas从当前行获取过去12个月的产品

时间:2018-03-15 12:55:36

标签: python pandas

我想要当前行中过去12个月的数据。

    Date   Open
    21/06/11    839.9
    22/06/11    853.35
    23/06/11    846.55
    24/06/11    874.15
    27/06/11    866.7
    28/06/11    878.9
    29/06/11    875.7
    30/06/11    888.7
    01/07/11    907
    04/07/11    874.4
    05/07/11    869.3
    06/07/11    848.85
    07/07/11    858
    08/07/11    873
    11/07/11    854
    12/07/11    847.5
    13/07/11    853.05
    14/07/11    863.3
    15/07/11    867.7
    18/07/11    871.9
    19/07/11    867.5
    20/07/11    886
    21/07/11    875.95
    22/07/11    866
    25/07/11    892
    26/07/11    888.25
    27/07/11    875
    28/07/11    855
    29/07/11    840
    01/08/11    838
    02/08/11    827.55
    03/08/11    826.75
    04/08/11    828
    05/08/11    799.5
    08/08/11    776.7
    09/08/11    753
    10/08/11    785.35
    11/08/11    768.35
    12/08/11    783
    16/08/11    760
    17/08/11    760.5
    18/08/11    757.7
    19/08/11    731.05
    22/08/11    731
    23/08/11    760.35
    24/08/11    764
    25/08/11    761.6
    26/08/11    751
    29/08/11    731.1
    30/08/11    765
    02/09/11    796.7
    05/09/11    794.5
    06/09/11    783.2
    07/09/11    824
    08/09/11    833.5
    09/09/11    852.15
    12/09/11    810.35
    13/09/11    813.2
    14/09/11    813.9
    15/09/11    833
    16/09/11    850
    19/09/11    825
    20/09/11    823
    21/09/11    850.9
    22/09/11    823.95
    23/09/11    773.9
    26/09/11    769.2
    27/09/11    774
    28/09/11    799.75
    29/09/11    790.5
    30/09/11    803.5
    03/10/11    791.2
    04/10/11    784
    05/10/11    772.55
    07/10/11    786.7
    10/10/11    804.25
    11/10/11    835
    12/10/11    829.4
    13/10/11    850
    14/10/11    842
    17/10/11    867
    18/10/11    825
    19/10/11    825.5
    20/10/11    834.85
    21/10/11    840
    24/10/11    848
    25/10/11    855
    26/10/11    879
    28/10/11    899.7
    31/10/11    898
    01/11/11    870.5
    02/11/11    855
    03/11/11    867.75
    04/11/11    905
    08/11/11    879
    09/11/11    890.05
    11/11/11    859
    14/11/11    891.4
    15/11/11    871
    16/11/11    859.1
    17/11/11    845.05
    18/11/11    800.3
    21/11/11    800
    22/11/11    788.1
    23/11/11    789.9
    24/11/11    775
    25/11/11    769.7
    28/11/11    765
    29/11/11    782
    30/11/11    756.7
    01/12/11    799
    02/12/11    797
    05/12/11    808.35
    07/12/11    807
    08/12/11    802
    09/12/11    769.9
    12/12/11    760.55
    13/12/11    723.9
    14/12/11    738
    15/12/11    731.9
    16/12/11    749
    19/12/11    719.2
    20/12/11    741.7
    21/12/11    727
    22/12/11    741.35
    23/12/11    760
    26/12/11    747.05
    27/12/11    766
    28/12/11    757.7
    29/12/11    733.65
    30/12/11    713
    02/01/12    696.8
    03/01/12    712.25
    04/01/12    727.4
    05/01/12    715
    06/01/12    697.05
    07/01/12    716.7
    09/01/12    714.45
    10/01/12    712
    11/01/12    737.9
    12/01/12    747.5
    13/01/12    742
    16/01/12    729.95
    17/01/12    716
    18/01/12    762
    19/01/12    789
    20/01/12    790
    23/01/12    755.3
    24/01/12    774.6
    25/01/12    788.7
    27/01/12    800
    30/01/12    813.9
    31/01/12    804.5
    01/02/12    818.9
    02/02/12    835
    03/02/12    830
    06/02/12    845.9
    07/02/12    842
    08/02/12    847
    09/02/12    856.75
    10/02/12    850.35
    13/02/12    841.1
    14/02/12    846.9
    15/02/12    854.2
    16/02/12    831
    17/02/12    822.05
    21/02/12    817.5
    22/02/12    848
    23/02/12    832
    24/02/12    833.5
    27/02/12    821.8
    28/02/12    789.05
    29/02/12    805.05
    01/03/12    811.8
    02/03/12    816.25
    03/03/12    811
    05/03/12    812.05
    06/03/12    797
    07/03/12    776.55
    09/03/12    775.3
    12/03/12    790
    13/03/12    803.45
    14/03/12    828
    15/03/12    818
    16/03/12    780
    19/03/12    781
    20/03/12    756.1
    21/03/12    760
    22/03/12    765.9
    23/03/12    743.8
    26/03/12    743.9
    27/03/12    738
    28/03/12    730
    29/03/12    718
    30/03/12    729.5
    02/04/12    749.35
    03/04/12    744.25
    04/04/12    745
    09/04/12    740.05
    10/04/12    746
    11/04/12    739
    12/04/12    733.3
    13/04/12    746.05
    16/04/12    747.1
    17/04/12    754.8

18/04/12    750
19/04/12    753.9
20/04/12    740.05
23/04/12    725.85
24/04/12    739
25/04/12    734.1
26/04/12    737.1
27/04/12    741.3
28/04/12    739.8
30/04/12    737.5
02/05/12    747.9
03/05/12    738.5
04/05/12    733.4
07/05/12    715
08/05/12    718
09/05/12    702
10/05/12    697.25
11/05/12    693
14/05/12    698
15/05/12    679
16/05/12    675
17/05/12    680.25
18/05/12    676.9
21/05/12    686.5
22/05/12    704.6
23/05/12    685.2
24/05/12    694
25/05/12    695
28/05/12    692
29/05/12    702.2
30/05/12    699.65
31/05/12    697
01/06/12    707.35
04/06/12    677
05/06/12    696
06/06/12    704.45
07/06/12    721.05
08/06/12    718
11/06/12    732.7
12/06/12    715
13/06/12    722.25
14/06/12    716
15/06/12    718.5
18/06/12    730.35
19/06/12    717
20/06/12    738
21/06/12    734
22/06/12    713.55
25/06/12    714.2
26/06/12    717.5
27/06/12    726.4
28/06/12    724.4
29/06/12    725.1
02/07/12    735.5
03/07/12    739.95
04/07/12    740
05/07/12    734.95
06/07/12    738
09/07/12    729
10/07/12    731.45
11/07/12    733.45
12/07/12    721.9
13/07/12    720
16/07/12    720
17/07/12    724.8
18/07/12    718
19/07/12    720.2
20/07/12    722.3
23/07/12    715
24/07/12    721
25/07/12    720.4
26/07/12    720.9
27/07/12    719
30/07/12    723
31/07/12    731.6
01/08/12    740.25
02/08/12    742.1
03/08/12    735
06/08/12    748.05
07/08/12    786.05
08/08/12    785.05
09/08/12    788.9
10/08/12    777.65
13/08/12    779.5
14/08/12    787.9
16/08/12    802.05
17/08/12    817.9
21/08/12    816
22/08/12    809.2
23/08/12    810.55
24/08/12    791.75
27/08/12    786
28/08/12    786.85
29/08/12    791
30/08/12    779.75
31/08/12    780
03/09/12    768
04/09/12    763.95
05/09/12    775.25
06/09/12    766.3
07/09/12    778.7
08/09/12    793.5
10/09/12    800
11/09/12    789.5
12/09/12    793.5
13/09/12    798.1
14/09/12    813
17/09/12    848.1
18/09/12    870.2

我尝试在这些行上使用某些东西,但没有找到解决方案:

df['val']= df['Open'].last('12M').transform('prod') 

如何获得结果?

2 个答案:

答案 0 :(得分:1)

如果您只需要df['Open']的最近12个月的产品,那么您可以这样做:

import pandas as pd
df['Date'] = pd.to_datetime(df['Date'])
df.set_index(['Date'], inplace=True)
df.sort_index(inplace=True)
df.tail(12).prod()

给你

Open   2.843636e+34
dtype: float64

答案 1 :(得分:1)

我认为您可以调整以下示例以获得所需内容:

# example with 7 days
import pandas as pd
dates = pd.date_range('1/1/2018', periods=7, freq='d')
values = [4,3,7,5,3,2,3]
df = pd.DataFrame({'col1':values}, index=dates)

# get product of last 2 days
df['col1'].last('2d').prod()